Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

function foo {
    local myvar=${bar}

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/… – Jiri Xichtkniha Oct 25 '13 at 13:48
up vote 18 down vote accepted

Quotes are needed in export foo="$var" or local foo="$var" (or 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 bash or sh emulation

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 in the cases above and also in bash as well as zsh (all versions) when in sh emulation.

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.

They are not needed in any shell when written:

foo=$var export foo

(that syntax being also compatible with the Bourne shell).

(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
$ bash -c 'IFS=; export a=$*; echo "$a"' bash a b
a b

My advise would be to always quote.

share|improve this answer
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
@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 at 4:09

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


bar="hi bye"

function foo {
  local myvar=${bar}
  printf "%s\n" $myvar
  printf "%s\n" "$myvar"


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 "%s\n" "$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>''.


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 '%s\n' hi bye
+ printf '%s\n' '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
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


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.