Are quotes needed for local variable assignment?












30















Can I safely omit quotes on the right side of a local assignment?



function foo {
local myvar=${bar}
stuff()
}


I'm mainly interested in bash, but any info on corner cases in other shells are welcome.










share|improve this question

























  • I think it makes no difference if it is on one line like you have it in your function. Assignments do not need quoting. See mpi-sb.mpg.de/departments/rg1/teaching/unixffb-ss98/…

    – jirib
    Oct 25 '13 at 13:48


















30















Can I safely omit quotes on the right side of a local assignment?



function foo {
local myvar=${bar}
stuff()
}


I'm mainly interested in bash, but any info on corner cases in other shells are welcome.










share|improve this question

























  • I think it makes no difference if it is on one line like you have it in your function. Assignments do not need quoting. See mpi-sb.mpg.de/departments/rg1/teaching/unixffb-ss98/…

    – jirib
    Oct 25 '13 at 13:48
















30












30








30


3






Can I safely omit quotes on the right side of a local assignment?



function foo {
local myvar=${bar}
stuff()
}


I'm mainly interested in bash, but any info on corner cases in other shells are welcome.










share|improve this question
















Can I safely omit quotes on the right side of a local assignment?



function foo {
local myvar=${bar}
stuff()
}


I'm mainly interested in bash, but any info on corner cases in other shells are welcome.







shell-script quoting assignment






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 21 '17 at 23:08









Mateusz Piotrowski

1,96421542




1,96421542










asked Oct 25 '13 at 13:17









rahmurahmu

10.4k1970112




10.4k1970112













  • I think it makes no difference if it is on one line like you have it in your function. Assignments do not need quoting. See mpi-sb.mpg.de/departments/rg1/teaching/unixffb-ss98/…

    – jirib
    Oct 25 '13 at 13:48





















  • I think it makes no difference if it is on one line like you have it in your function. Assignments do not need quoting. See mpi-sb.mpg.de/departments/rg1/teaching/unixffb-ss98/…

    – jirib
    Oct 25 '13 at 13:48



















I think it makes no difference if it is on one line like you have it in your function. Assignments do not need quoting. See mpi-sb.mpg.de/departments/rg1/teaching/unixffb-ss98/…

– jirib
Oct 25 '13 at 13:48







I think it makes no difference if it is on one line like you have it in your function. Assignments do not need quoting. See mpi-sb.mpg.de/departments/rg1/teaching/unixffb-ss98/…

– jirib
Oct 25 '13 at 13:48












2 Answers
2






active

oldest

votes


















33














Quotes are needed in export foo="$var" or local foo="$var" (or readonly, typeset, declare and other variable declaring commands) in:




  • dash

  • the sh of NetBSD (also based on the Almquist shell).

  • The sh of FreeBSD 9.2 or older (see the change in 9.3)

  • yash


  • zsh with versions prior to 5.1 in ksh or sh emulation (or for export var="$(cmd)" where zsh would perform word splitting otherwise (not globbing)).


As otherwise the variable expansion would be subject to word splitting and/or filename generation like in any argument to any other command.



And are not needed in bash or ksh or the sh of FreeBSD 9.3 or newer (where the command is somehow parsed like some sort of assignment) nor zsh (where word splitting and filename generation is not done implicitly upon variable expansion).



They are needed in every shell (except zsh) though in things like:



a="b=some value"
export "$a"


Or more generally, if anything left of the = (including the =) is quoted or the result of some expansion (like export 'foo'="$var", export foo="$var" or export foo$((n+=1))="$var" (that $((...)) should also be quoted actually)...). Or in other words when the argument to export wouldn't be a valid variable assignment if written without the export.



If the export/local command name itself is quoted (even in part like "export" a="$b", 'ex'port a="$b", export a="$b", or even ""export a="$b"), the quotes around $b are needed except in AT&T ksh and mksh.



If export/local or some part of it is the result of some expansion (like in cmd=export; "$cmd" a="$b" or even export$(:) a="$b") or if export/local is not the first word on the command line (as in dryrun=; $dryrun export a="$b"), then the quotes are needed in every shell. In the case of > /dev/null export a="$b" however, the quotes are not needed in bash/AT&T ksh and zsh (they are in pdksh and some of its derivatives).



For command export a="$b", the quotes are needed in every shell but mksh and ksh93 (with the same caveats about command and export not being the result of some expansion).



They are not needed in any shell when written:



foo=$var export foo


(that syntax being also compatible with the Bourne shell but in the case of bash and zsh only working when in sh emulation).



(note that var=value local var shouldn't be used as the behaviour varies across shells).



Also beware of this special case with bash:



$ bash -c 'IFS=; export a="$*"; echo "$a"' bash a b
ab
$ bash -c 'IFS=; export a=$*; echo "$a"' bash a b
a b


My advise would be to always quote.






share|improve this answer





















  • 3





    Note that in zsh quotes are needed for local foo="$(cmd)" because wordsplitting (but not filename generation) is performed for unquoted command substitutions (but not for unquoted parameter expansions), unless KSH_TYPESET is enabled, in which case quotes aren't needed. Make sense? No? Then always quote everything unless you know exactly what you're doing.

    – Matt
    Oct 25 '13 at 17:39






  • 2





    @Matt, I love your conclusion. :D It's funny, most of what I've learned of shell scripting came from this stackexchange, so that I didn't realize that always quote your variables is not common knowledge amongst script writers. I'm finding I have a lot of fixing up to do of existing production scripts written by people who didn't quote, and didn't know exactly what they were doing....

    – Wildcard
    Jan 6 '16 at 4:09



















2














I generally quote any usage of variables where there might be characters such as white spaces. Otherwise you'll run into problems like this:



#!/bin/bash

bar="hi bye"

function foo {
local myvar=${bar}
printf "%sn" $myvar
printf "%sn" "$myvar"
}

foo


The usage of the variable in an assignment doesn't appear to need the quotes, but when you go to use it such as in the printf you'll need it quoted there:



  printf "%sn" "$myvar"


NOTE: Remember that the variable $IFS is what governs what the separator characters are.



IFS    The  Internal  Field  Separator that is used for word splitting after 
expansion and to split lines into words with the read builtin command.
The default value is ``<space><tab><newline>''.


Example



With debugging enabled in Bash we can see what's happening behind the scenes.



$ bash -x cmd.bash 
+ bar='hi bye'
+ foo
+ local 'myvar=hi bye'
+ printf '%sn' hi bye
hi
bye
+ printf '%sn' 'hi bye'
hi bye


In the above we can see that the variable, $bar was handed off fine to $myvar but then when we went to use $myvar we had to be cognoscente of the contents of $myvar when we went to use it.






share|improve this answer



















  • 2





    word splitting is not the only problem with unquoted variables, you have to consider filename generation (aka globbing) as well (though that (both) doesn't apply in variable assignments and for bash and ksh in local/typeset... special builtins).

    – Stéphane Chazelas
    Oct 25 '13 at 13:58











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',
autoActivateHeartbeat: false,
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%2f97560%2fare-quotes-needed-for-local-variable-assignment%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









33














Quotes are needed in export foo="$var" or local foo="$var" (or readonly, typeset, declare and other variable declaring commands) in:




  • dash

  • the sh of NetBSD (also based on the Almquist shell).

  • The sh of FreeBSD 9.2 or older (see the change in 9.3)

  • yash


  • zsh with versions prior to 5.1 in ksh or sh emulation (or for export var="$(cmd)" where zsh would perform word splitting otherwise (not globbing)).


As otherwise the variable expansion would be subject to word splitting and/or filename generation like in any argument to any other command.



And are not needed in bash or ksh or the sh of FreeBSD 9.3 or newer (where the command is somehow parsed like some sort of assignment) nor zsh (where word splitting and filename generation is not done implicitly upon variable expansion).



They are needed in every shell (except zsh) though in things like:



a="b=some value"
export "$a"


Or more generally, if anything left of the = (including the =) is quoted or the result of some expansion (like export 'foo'="$var", export foo="$var" or export foo$((n+=1))="$var" (that $((...)) should also be quoted actually)...). Or in other words when the argument to export wouldn't be a valid variable assignment if written without the export.



If the export/local command name itself is quoted (even in part like "export" a="$b", 'ex'port a="$b", export a="$b", or even ""export a="$b"), the quotes around $b are needed except in AT&T ksh and mksh.



If export/local or some part of it is the result of some expansion (like in cmd=export; "$cmd" a="$b" or even export$(:) a="$b") or if export/local is not the first word on the command line (as in dryrun=; $dryrun export a="$b"), then the quotes are needed in every shell. In the case of > /dev/null export a="$b" however, the quotes are not needed in bash/AT&T ksh and zsh (they are in pdksh and some of its derivatives).



For command export a="$b", the quotes are needed in every shell but mksh and ksh93 (with the same caveats about command and export not being the result of some expansion).



They are not needed in any shell when written:



foo=$var export foo


(that syntax being also compatible with the Bourne shell but in the case of bash and zsh only working when in sh emulation).



(note that var=value local var shouldn't be used as the behaviour varies across shells).



Also beware of this special case with bash:



$ bash -c 'IFS=; export a="$*"; echo "$a"' bash a b
ab
$ bash -c 'IFS=; export a=$*; echo "$a"' bash a b
a b


My advise would be to always quote.






share|improve this answer





















  • 3





    Note that in zsh quotes are needed for local foo="$(cmd)" because wordsplitting (but not filename generation) is performed for unquoted command substitutions (but not for unquoted parameter expansions), unless KSH_TYPESET is enabled, in which case quotes aren't needed. Make sense? No? Then always quote everything unless you know exactly what you're doing.

    – Matt
    Oct 25 '13 at 17:39






  • 2





    @Matt, I love your conclusion. :D It's funny, most of what I've learned of shell scripting came from this stackexchange, so that I didn't realize that always quote your variables is not common knowledge amongst script writers. I'm finding I have a lot of fixing up to do of existing production scripts written by people who didn't quote, and didn't know exactly what they were doing....

    – Wildcard
    Jan 6 '16 at 4:09
















33














Quotes are needed in export foo="$var" or local foo="$var" (or readonly, typeset, declare and other variable declaring commands) in:




  • dash

  • the sh of NetBSD (also based on the Almquist shell).

  • The sh of FreeBSD 9.2 or older (see the change in 9.3)

  • yash


  • zsh with versions prior to 5.1 in ksh or sh emulation (or for export var="$(cmd)" where zsh would perform word splitting otherwise (not globbing)).


As otherwise the variable expansion would be subject to word splitting and/or filename generation like in any argument to any other command.



And are not needed in bash or ksh or the sh of FreeBSD 9.3 or newer (where the command is somehow parsed like some sort of assignment) nor zsh (where word splitting and filename generation is not done implicitly upon variable expansion).



They are needed in every shell (except zsh) though in things like:



a="b=some value"
export "$a"


Or more generally, if anything left of the = (including the =) is quoted or the result of some expansion (like export 'foo'="$var", export foo="$var" or export foo$((n+=1))="$var" (that $((...)) should also be quoted actually)...). Or in other words when the argument to export wouldn't be a valid variable assignment if written without the export.



If the export/local command name itself is quoted (even in part like "export" a="$b", 'ex'port a="$b", export a="$b", or even ""export a="$b"), the quotes around $b are needed except in AT&T ksh and mksh.



If export/local or some part of it is the result of some expansion (like in cmd=export; "$cmd" a="$b" or even export$(:) a="$b") or if export/local is not the first word on the command line (as in dryrun=; $dryrun export a="$b"), then the quotes are needed in every shell. In the case of > /dev/null export a="$b" however, the quotes are not needed in bash/AT&T ksh and zsh (they are in pdksh and some of its derivatives).



For command export a="$b", the quotes are needed in every shell but mksh and ksh93 (with the same caveats about command and export not being the result of some expansion).



They are not needed in any shell when written:



foo=$var export foo


(that syntax being also compatible with the Bourne shell but in the case of bash and zsh only working when in sh emulation).



(note that var=value local var shouldn't be used as the behaviour varies across shells).



Also beware of this special case with bash:



$ bash -c 'IFS=; export a="$*"; echo "$a"' bash a b
ab
$ bash -c 'IFS=; export a=$*; echo "$a"' bash a b
a b


My advise would be to always quote.






share|improve this answer





















  • 3





    Note that in zsh quotes are needed for local foo="$(cmd)" because wordsplitting (but not filename generation) is performed for unquoted command substitutions (but not for unquoted parameter expansions), unless KSH_TYPESET is enabled, in which case quotes aren't needed. Make sense? No? Then always quote everything unless you know exactly what you're doing.

    – Matt
    Oct 25 '13 at 17:39






  • 2





    @Matt, I love your conclusion. :D It's funny, most of what I've learned of shell scripting came from this stackexchange, so that I didn't realize that always quote your variables is not common knowledge amongst script writers. I'm finding I have a lot of fixing up to do of existing production scripts written by people who didn't quote, and didn't know exactly what they were doing....

    – Wildcard
    Jan 6 '16 at 4:09














33












33








33







Quotes are needed in export foo="$var" or local foo="$var" (or readonly, typeset, declare and other variable declaring commands) in:




  • dash

  • the sh of NetBSD (also based on the Almquist shell).

  • The sh of FreeBSD 9.2 or older (see the change in 9.3)

  • yash


  • zsh with versions prior to 5.1 in ksh or sh emulation (or for export var="$(cmd)" where zsh would perform word splitting otherwise (not globbing)).


As otherwise the variable expansion would be subject to word splitting and/or filename generation like in any argument to any other command.



And are not needed in bash or ksh or the sh of FreeBSD 9.3 or newer (where the command is somehow parsed like some sort of assignment) nor zsh (where word splitting and filename generation is not done implicitly upon variable expansion).



They are needed in every shell (except zsh) though in things like:



a="b=some value"
export "$a"


Or more generally, if anything left of the = (including the =) is quoted or the result of some expansion (like export 'foo'="$var", export foo="$var" or export foo$((n+=1))="$var" (that $((...)) should also be quoted actually)...). Or in other words when the argument to export wouldn't be a valid variable assignment if written without the export.



If the export/local command name itself is quoted (even in part like "export" a="$b", 'ex'port a="$b", export a="$b", or even ""export a="$b"), the quotes around $b are needed except in AT&T ksh and mksh.



If export/local or some part of it is the result of some expansion (like in cmd=export; "$cmd" a="$b" or even export$(:) a="$b") or if export/local is not the first word on the command line (as in dryrun=; $dryrun export a="$b"), then the quotes are needed in every shell. In the case of > /dev/null export a="$b" however, the quotes are not needed in bash/AT&T ksh and zsh (they are in pdksh and some of its derivatives).



For command export a="$b", the quotes are needed in every shell but mksh and ksh93 (with the same caveats about command and export not being the result of some expansion).



They are not needed in any shell when written:



foo=$var export foo


(that syntax being also compatible with the Bourne shell but in the case of bash and zsh only working when in sh emulation).



(note that var=value local var shouldn't be used as the behaviour varies across shells).



Also beware of this special case with bash:



$ bash -c 'IFS=; export a="$*"; echo "$a"' bash a b
ab
$ bash -c 'IFS=; export a=$*; echo "$a"' bash a b
a b


My advise would be to always quote.






share|improve this answer















Quotes are needed in export foo="$var" or local foo="$var" (or readonly, typeset, declare and other variable declaring commands) in:




  • dash

  • the sh of NetBSD (also based on the Almquist shell).

  • The sh of FreeBSD 9.2 or older (see the change in 9.3)

  • yash


  • zsh with versions prior to 5.1 in ksh or sh emulation (or for export var="$(cmd)" where zsh would perform word splitting otherwise (not globbing)).


As otherwise the variable expansion would be subject to word splitting and/or filename generation like in any argument to any other command.



And are not needed in bash or ksh or the sh of FreeBSD 9.3 or newer (where the command is somehow parsed like some sort of assignment) nor zsh (where word splitting and filename generation is not done implicitly upon variable expansion).



They are needed in every shell (except zsh) though in things like:



a="b=some value"
export "$a"


Or more generally, if anything left of the = (including the =) is quoted or the result of some expansion (like export 'foo'="$var", export foo="$var" or export foo$((n+=1))="$var" (that $((...)) should also be quoted actually)...). Or in other words when the argument to export wouldn't be a valid variable assignment if written without the export.



If the export/local command name itself is quoted (even in part like "export" a="$b", 'ex'port a="$b", export a="$b", or even ""export a="$b"), the quotes around $b are needed except in AT&T ksh and mksh.



If export/local or some part of it is the result of some expansion (like in cmd=export; "$cmd" a="$b" or even export$(:) a="$b") or if export/local is not the first word on the command line (as in dryrun=; $dryrun export a="$b"), then the quotes are needed in every shell. In the case of > /dev/null export a="$b" however, the quotes are not needed in bash/AT&T ksh and zsh (they are in pdksh and some of its derivatives).



For command export a="$b", the quotes are needed in every shell but mksh and ksh93 (with the same caveats about command and export not being the result of some expansion).



They are not needed in any shell when written:



foo=$var export foo


(that syntax being also compatible with the Bourne shell but in the case of bash and zsh only working when in sh emulation).



(note that var=value local var shouldn't be used as the behaviour varies across shells).



Also beware of this special case with bash:



$ bash -c 'IFS=; export a="$*"; echo "$a"' bash a b
ab
$ bash -c 'IFS=; export a=$*; echo "$a"' bash a b
a b


My advise would be to always quote.







share|improve this answer














share|improve this answer



share|improve this answer








edited 11 mins ago

























answered Oct 25 '13 at 13:46









Stéphane ChazelasStéphane Chazelas

305k57577931




305k57577931








  • 3





    Note that in zsh quotes are needed for local foo="$(cmd)" because wordsplitting (but not filename generation) is performed for unquoted command substitutions (but not for unquoted parameter expansions), unless KSH_TYPESET is enabled, in which case quotes aren't needed. Make sense? No? Then always quote everything unless you know exactly what you're doing.

    – Matt
    Oct 25 '13 at 17:39






  • 2





    @Matt, I love your conclusion. :D It's funny, most of what I've learned of shell scripting came from this stackexchange, so that I didn't realize that always quote your variables is not common knowledge amongst script writers. I'm finding I have a lot of fixing up to do of existing production scripts written by people who didn't quote, and didn't know exactly what they were doing....

    – Wildcard
    Jan 6 '16 at 4:09














  • 3





    Note that in zsh quotes are needed for local foo="$(cmd)" because wordsplitting (but not filename generation) is performed for unquoted command substitutions (but not for unquoted parameter expansions), unless KSH_TYPESET is enabled, in which case quotes aren't needed. Make sense? No? Then always quote everything unless you know exactly what you're doing.

    – Matt
    Oct 25 '13 at 17:39






  • 2





    @Matt, I love your conclusion. :D It's funny, most of what I've learned of shell scripting came from this stackexchange, so that I didn't realize that always quote your variables is not common knowledge amongst script writers. I'm finding I have a lot of fixing up to do of existing production scripts written by people who didn't quote, and didn't know exactly what they were doing....

    – Wildcard
    Jan 6 '16 at 4:09








3




3





Note that in zsh quotes are needed for local foo="$(cmd)" because wordsplitting (but not filename generation) is performed for unquoted command substitutions (but not for unquoted parameter expansions), unless KSH_TYPESET is enabled, in which case quotes aren't needed. Make sense? No? Then always quote everything unless you know exactly what you're doing.

– Matt
Oct 25 '13 at 17:39





Note that in zsh quotes are needed for local foo="$(cmd)" because wordsplitting (but not filename generation) is performed for unquoted command substitutions (but not for unquoted parameter expansions), unless KSH_TYPESET is enabled, in which case quotes aren't needed. Make sense? No? Then always quote everything unless you know exactly what you're doing.

– Matt
Oct 25 '13 at 17:39




2




2





@Matt, I love your conclusion. :D It's funny, most of what I've learned of shell scripting came from this stackexchange, so that I didn't realize that always quote your variables is not common knowledge amongst script writers. I'm finding I have a lot of fixing up to do of existing production scripts written by people who didn't quote, and didn't know exactly what they were doing....

– Wildcard
Jan 6 '16 at 4:09





@Matt, I love your conclusion. :D It's funny, most of what I've learned of shell scripting came from this stackexchange, so that I didn't realize that always quote your variables is not common knowledge amongst script writers. I'm finding I have a lot of fixing up to do of existing production scripts written by people who didn't quote, and didn't know exactly what they were doing....

– Wildcard
Jan 6 '16 at 4:09













2














I generally quote any usage of variables where there might be characters such as white spaces. Otherwise you'll run into problems like this:



#!/bin/bash

bar="hi bye"

function foo {
local myvar=${bar}
printf "%sn" $myvar
printf "%sn" "$myvar"
}

foo


The usage of the variable in an assignment doesn't appear to need the quotes, but when you go to use it such as in the printf you'll need it quoted there:



  printf "%sn" "$myvar"


NOTE: Remember that the variable $IFS is what governs what the separator characters are.



IFS    The  Internal  Field  Separator that is used for word splitting after 
expansion and to split lines into words with the read builtin command.
The default value is ``<space><tab><newline>''.


Example



With debugging enabled in Bash we can see what's happening behind the scenes.



$ bash -x cmd.bash 
+ bar='hi bye'
+ foo
+ local 'myvar=hi bye'
+ printf '%sn' hi bye
hi
bye
+ printf '%sn' 'hi bye'
hi bye


In the above we can see that the variable, $bar was handed off fine to $myvar but then when we went to use $myvar we had to be cognoscente of the contents of $myvar when we went to use it.






share|improve this answer



















  • 2





    word splitting is not the only problem with unquoted variables, you have to consider filename generation (aka globbing) as well (though that (both) doesn't apply in variable assignments and for bash and ksh in local/typeset... special builtins).

    – Stéphane Chazelas
    Oct 25 '13 at 13:58
















2














I generally quote any usage of variables where there might be characters such as white spaces. Otherwise you'll run into problems like this:



#!/bin/bash

bar="hi bye"

function foo {
local myvar=${bar}
printf "%sn" $myvar
printf "%sn" "$myvar"
}

foo


The usage of the variable in an assignment doesn't appear to need the quotes, but when you go to use it such as in the printf you'll need it quoted there:



  printf "%sn" "$myvar"


NOTE: Remember that the variable $IFS is what governs what the separator characters are.



IFS    The  Internal  Field  Separator that is used for word splitting after 
expansion and to split lines into words with the read builtin command.
The default value is ``<space><tab><newline>''.


Example



With debugging enabled in Bash we can see what's happening behind the scenes.



$ bash -x cmd.bash 
+ bar='hi bye'
+ foo
+ local 'myvar=hi bye'
+ printf '%sn' hi bye
hi
bye
+ printf '%sn' 'hi bye'
hi bye


In the above we can see that the variable, $bar was handed off fine to $myvar but then when we went to use $myvar we had to be cognoscente of the contents of $myvar when we went to use it.






share|improve this answer



















  • 2





    word splitting is not the only problem with unquoted variables, you have to consider filename generation (aka globbing) as well (though that (both) doesn't apply in variable assignments and for bash and ksh in local/typeset... special builtins).

    – Stéphane Chazelas
    Oct 25 '13 at 13:58














2












2








2







I generally quote any usage of variables where there might be characters such as white spaces. Otherwise you'll run into problems like this:



#!/bin/bash

bar="hi bye"

function foo {
local myvar=${bar}
printf "%sn" $myvar
printf "%sn" "$myvar"
}

foo


The usage of the variable in an assignment doesn't appear to need the quotes, but when you go to use it such as in the printf you'll need it quoted there:



  printf "%sn" "$myvar"


NOTE: Remember that the variable $IFS is what governs what the separator characters are.



IFS    The  Internal  Field  Separator that is used for word splitting after 
expansion and to split lines into words with the read builtin command.
The default value is ``<space><tab><newline>''.


Example



With debugging enabled in Bash we can see what's happening behind the scenes.



$ bash -x cmd.bash 
+ bar='hi bye'
+ foo
+ local 'myvar=hi bye'
+ printf '%sn' hi bye
hi
bye
+ printf '%sn' 'hi bye'
hi bye


In the above we can see that the variable, $bar was handed off fine to $myvar but then when we went to use $myvar we had to be cognoscente of the contents of $myvar when we went to use it.






share|improve this answer













I generally quote any usage of variables where there might be characters such as white spaces. Otherwise you'll run into problems like this:



#!/bin/bash

bar="hi bye"

function foo {
local myvar=${bar}
printf "%sn" $myvar
printf "%sn" "$myvar"
}

foo


The usage of the variable in an assignment doesn't appear to need the quotes, but when you go to use it such as in the printf you'll need it quoted there:



  printf "%sn" "$myvar"


NOTE: Remember that the variable $IFS is what governs what the separator characters are.



IFS    The  Internal  Field  Separator that is used for word splitting after 
expansion and to split lines into words with the read builtin command.
The default value is ``<space><tab><newline>''.


Example



With debugging enabled in Bash we can see what's happening behind the scenes.



$ bash -x cmd.bash 
+ bar='hi bye'
+ foo
+ local 'myvar=hi bye'
+ printf '%sn' hi bye
hi
bye
+ printf '%sn' 'hi bye'
hi bye


In the above we can see that the variable, $bar was handed off fine to $myvar but then when we went to use $myvar we had to be cognoscente of the contents of $myvar when we went to use it.







share|improve this answer












share|improve this answer



share|improve this answer










answered Oct 25 '13 at 13:26









slmslm

251k69529685




251k69529685








  • 2





    word splitting is not the only problem with unquoted variables, you have to consider filename generation (aka globbing) as well (though that (both) doesn't apply in variable assignments and for bash and ksh in local/typeset... special builtins).

    – Stéphane Chazelas
    Oct 25 '13 at 13:58














  • 2





    word splitting is not the only problem with unquoted variables, you have to consider filename generation (aka globbing) as well (though that (both) doesn't apply in variable assignments and for bash and ksh in local/typeset... special builtins).

    – Stéphane Chazelas
    Oct 25 '13 at 13:58








2




2





word splitting is not the only problem with unquoted variables, you have to consider filename generation (aka globbing) as well (though that (both) doesn't apply in variable assignments and for bash and ksh in local/typeset... special builtins).

– Stéphane Chazelas
Oct 25 '13 at 13:58





word splitting is not the only problem with unquoted variables, you have to consider filename generation (aka globbing) as well (though that (both) doesn't apply in variable assignments and for bash and ksh in local/typeset... special builtins).

– Stéphane Chazelas
Oct 25 '13 at 13:58


















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f97560%2fare-quotes-needed-for-local-variable-assignment%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

Loup dans la culture

How to solve the problem of ntp “Unable to contact time server” from KDE?

ASUS Zenbook UX433/UX333 — Configure Touchpad-embedded numpad on Linux