Prepare arguments containing quoted string in variable
up vote
3
down vote
favorite
In a Bash script, I call a program like this in several places:
numfmt --suffix=" B" --grouping 231210893
Where the number is different every time, but the other parameters stay the same.
I would now like to move the other parameters out of the many different calls, so they are centrally defined and can be easily changed. My attempt was like this:
NUMFMT='--suffix=" B" --grouping'
...
numfmt $NUMFMT 231210893
Unfortunately, this doesn't work. The quote signs are removed at some point, and numfmt
complains about an uninterpretable extra argument B
. I tried plenty of other versions, using other quotes both in the definition and in the use of NUMFMT
, to no avail.
How do I do this properly? And if it's not too complicated, I would also like to understand why my version doesn't work and (hopefully) another one does.
bash string bash-expansion
add a comment |
up vote
3
down vote
favorite
In a Bash script, I call a program like this in several places:
numfmt --suffix=" B" --grouping 231210893
Where the number is different every time, but the other parameters stay the same.
I would now like to move the other parameters out of the many different calls, so they are centrally defined and can be easily changed. My attempt was like this:
NUMFMT='--suffix=" B" --grouping'
...
numfmt $NUMFMT 231210893
Unfortunately, this doesn't work. The quote signs are removed at some point, and numfmt
complains about an uninterpretable extra argument B
. I tried plenty of other versions, using other quotes both in the definition and in the use of NUMFMT
, to no avail.
How do I do this properly? And if it's not too complicated, I would also like to understand why my version doesn't work and (hopefully) another one does.
bash string bash-expansion
3
See Why does my shell script choke on whitespace or other special characters? - in particular, the section How do I store a command in a variable? in @Gilles answer
– steeldriver
Nov 18 at 0:57
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
In a Bash script, I call a program like this in several places:
numfmt --suffix=" B" --grouping 231210893
Where the number is different every time, but the other parameters stay the same.
I would now like to move the other parameters out of the many different calls, so they are centrally defined and can be easily changed. My attempt was like this:
NUMFMT='--suffix=" B" --grouping'
...
numfmt $NUMFMT 231210893
Unfortunately, this doesn't work. The quote signs are removed at some point, and numfmt
complains about an uninterpretable extra argument B
. I tried plenty of other versions, using other quotes both in the definition and in the use of NUMFMT
, to no avail.
How do I do this properly? And if it's not too complicated, I would also like to understand why my version doesn't work and (hopefully) another one does.
bash string bash-expansion
In a Bash script, I call a program like this in several places:
numfmt --suffix=" B" --grouping 231210893
Where the number is different every time, but the other parameters stay the same.
I would now like to move the other parameters out of the many different calls, so they are centrally defined and can be easily changed. My attempt was like this:
NUMFMT='--suffix=" B" --grouping'
...
numfmt $NUMFMT 231210893
Unfortunately, this doesn't work. The quote signs are removed at some point, and numfmt
complains about an uninterpretable extra argument B
. I tried plenty of other versions, using other quotes both in the definition and in the use of NUMFMT
, to no avail.
How do I do this properly? And if it's not too complicated, I would also like to understand why my version doesn't work and (hopefully) another one does.
bash string bash-expansion
bash string bash-expansion
edited Nov 18 at 13:41
Peter Mortensen
85758
85758
asked Nov 18 at 0:42
A. Donda
1558
1558
3
See Why does my shell script choke on whitespace or other special characters? - in particular, the section How do I store a command in a variable? in @Gilles answer
– steeldriver
Nov 18 at 0:57
add a comment |
3
See Why does my shell script choke on whitespace or other special characters? - in particular, the section How do I store a command in a variable? in @Gilles answer
– steeldriver
Nov 18 at 0:57
3
3
See Why does my shell script choke on whitespace or other special characters? - in particular, the section How do I store a command in a variable? in @Gilles answer
– steeldriver
Nov 18 at 0:57
See Why does my shell script choke on whitespace or other special characters? - in particular, the section How do I store a command in a variable? in @Gilles answer
– steeldriver
Nov 18 at 0:57
add a comment |
2 Answers
2
active
oldest
votes
up vote
6
down vote
accepted
Try arrays:
NUMFMT=( --suffix=" B" '--grouping' )
....
numfmt "${NUMFMT[@]}" 231210893
I had to remove the"
s in the first argument, but now it works perfectly. Thanks!
– A. Donda
Nov 18 at 1:38
add a comment |
up vote
0
down vote
Wouldn't that be an excellent case for an alias
?
$ alias nfmtB='numfmt --suffix=" B" --grouping'
$ nfmtB 324235345656
324.235.345.656 B
Good idea but with a little catch: command aliases are disabled in scripts by default unless enabled withshopt -s expand_aliases
.
– David Foerster
Nov 18 at 12:29
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
Try arrays:
NUMFMT=( --suffix=" B" '--grouping' )
....
numfmt "${NUMFMT[@]}" 231210893
I had to remove the"
s in the first argument, but now it works perfectly. Thanks!
– A. Donda
Nov 18 at 1:38
add a comment |
up vote
6
down vote
accepted
Try arrays:
NUMFMT=( --suffix=" B" '--grouping' )
....
numfmt "${NUMFMT[@]}" 231210893
I had to remove the"
s in the first argument, but now it works perfectly. Thanks!
– A. Donda
Nov 18 at 1:38
add a comment |
up vote
6
down vote
accepted
up vote
6
down vote
accepted
Try arrays:
NUMFMT=( --suffix=" B" '--grouping' )
....
numfmt "${NUMFMT[@]}" 231210893
Try arrays:
NUMFMT=( --suffix=" B" '--grouping' )
....
numfmt "${NUMFMT[@]}" 231210893
edited Nov 18 at 3:26
Filipe Brandenburger
6,6801732
6,6801732
answered Nov 18 at 1:21
Isaac
10.1k11445
10.1k11445
I had to remove the"
s in the first argument, but now it works perfectly. Thanks!
– A. Donda
Nov 18 at 1:38
add a comment |
I had to remove the"
s in the first argument, but now it works perfectly. Thanks!
– A. Donda
Nov 18 at 1:38
I had to remove the
"
s in the first argument, but now it works perfectly. Thanks!– A. Donda
Nov 18 at 1:38
I had to remove the
"
s in the first argument, but now it works perfectly. Thanks!– A. Donda
Nov 18 at 1:38
add a comment |
up vote
0
down vote
Wouldn't that be an excellent case for an alias
?
$ alias nfmtB='numfmt --suffix=" B" --grouping'
$ nfmtB 324235345656
324.235.345.656 B
Good idea but with a little catch: command aliases are disabled in scripts by default unless enabled withshopt -s expand_aliases
.
– David Foerster
Nov 18 at 12:29
add a comment |
up vote
0
down vote
Wouldn't that be an excellent case for an alias
?
$ alias nfmtB='numfmt --suffix=" B" --grouping'
$ nfmtB 324235345656
324.235.345.656 B
Good idea but with a little catch: command aliases are disabled in scripts by default unless enabled withshopt -s expand_aliases
.
– David Foerster
Nov 18 at 12:29
add a comment |
up vote
0
down vote
up vote
0
down vote
Wouldn't that be an excellent case for an alias
?
$ alias nfmtB='numfmt --suffix=" B" --grouping'
$ nfmtB 324235345656
324.235.345.656 B
Wouldn't that be an excellent case for an alias
?
$ alias nfmtB='numfmt --suffix=" B" --grouping'
$ nfmtB 324235345656
324.235.345.656 B
answered Nov 18 at 11:52
RudiC
3,5171312
3,5171312
Good idea but with a little catch: command aliases are disabled in scripts by default unless enabled withshopt -s expand_aliases
.
– David Foerster
Nov 18 at 12:29
add a comment |
Good idea but with a little catch: command aliases are disabled in scripts by default unless enabled withshopt -s expand_aliases
.
– David Foerster
Nov 18 at 12:29
Good idea but with a little catch: command aliases are disabled in scripts by default unless enabled with
shopt -s expand_aliases
.– David Foerster
Nov 18 at 12:29
Good idea but with a little catch: command aliases are disabled in scripts by default unless enabled with
shopt -s expand_aliases
.– David Foerster
Nov 18 at 12:29
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482438%2fprepare-arguments-containing-quoted-string-in-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
See Why does my shell script choke on whitespace or other special characters? - in particular, the section How do I store a command in a variable? in @Gilles answer
– steeldriver
Nov 18 at 0:57