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
2 days ago
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 yesterday
Peter Mortensen
85358
85358
asked 2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
yesterday
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
yesterday
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
yesterday
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
yesterday
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 yesterday
Filipe Brandenburger
6,1041625
6,1041625
answered 2 days ago
Isaac
9,58411443
9,58411443
I had to remove the"s in the first argument, but now it works perfectly. Thanks!
– A. Donda
yesterday
add a comment |
I had to remove the"s in the first argument, but now it works perfectly. Thanks!
– A. Donda
yesterday
I had to remove the
"s in the first argument, but now it works perfectly. Thanks!– A. Donda
yesterday
I had to remove the
"s in the first argument, but now it works perfectly. Thanks!– A. Donda
yesterday
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
yesterday
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
yesterday
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 yesterday
RudiC
3,0811211
3,0811211
Good idea but with a little catch: command aliases are disabled in scripts by default unless enabled withshopt -s expand_aliases.
– David Foerster
yesterday
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
yesterday
Good idea but with a little catch: command aliases are disabled in scripts by default unless enabled with
shopt -s expand_aliases.– David Foerster
yesterday
Good idea but with a little catch: command aliases are disabled in scripts by default unless enabled with
shopt -s expand_aliases.– David Foerster
yesterday
add a comment |
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
2 days ago