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.










share|improve this question




















  • 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















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.










share|improve this question




















  • 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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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










2 Answers
2






active

oldest

votes

















up vote
6
down vote



accepted










Try arrays:



NUMFMT=( --suffix=" B"   '--grouping' )
....
numfmt "${NUMFMT[@]}" 231210893





share|improve this answer























  • I had to remove the "s in the first argument, but now it works perfectly. Thanks!
    – A. Donda
    Nov 18 at 1:38




















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





share|improve this answer





















  • 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













Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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





share|improve this answer























  • I had to remove the "s in the first argument, but now it works perfectly. Thanks!
    – A. Donda
    Nov 18 at 1:38

















up vote
6
down vote



accepted










Try arrays:



NUMFMT=( --suffix=" B"   '--grouping' )
....
numfmt "${NUMFMT[@]}" 231210893





share|improve this answer























  • I had to remove the "s in the first argument, but now it works perfectly. Thanks!
    – A. Donda
    Nov 18 at 1:38















up vote
6
down vote



accepted







up vote
6
down vote



accepted






Try arrays:



NUMFMT=( --suffix=" B"   '--grouping' )
....
numfmt "${NUMFMT[@]}" 231210893





share|improve this answer














Try arrays:



NUMFMT=( --suffix=" B"   '--grouping' )
....
numfmt "${NUMFMT[@]}" 231210893






share|improve this answer














share|improve this answer



share|improve this answer








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




















  • 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














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





share|improve this answer





















  • 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

















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





share|improve this answer





















  • 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















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





share|improve this answer












Wouldn't that be an excellent case for an alias?



$ alias nfmtB='numfmt --suffix=" B" --grouping'
$ nfmtB 324235345656
324.235.345.656 B






share|improve this answer












share|improve this answer



share|improve this answer










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 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


















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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Актюбинская область

QoS: MAC-Priority for clients behind a repeater

AnyDesk - Fatal Program Failure