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
    2 days ago















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
    2 days ago













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














  • 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










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
    yesterday




















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
    yesterday













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
    yesterday

















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
    yesterday















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




















  • 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














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
    yesterday

















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
    yesterday















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 yesterday









RudiC

3,0811211




3,0811211












  • 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


















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




















 

draft saved


draft discarded



















































 


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

Mouse cursor on multiple screens with different PPI

Agildo Ribeiro

Sometime when accessing a menu: “Ubuntu 16.04 has experienced an internal error”