Spaces in variable assignments in shell scripts
That very much depends on the shell. If we only look at the 4 main shell families (Bourne, csh, rc, fish):
Bourne family
That is the Bourne shell and all its variants and ksh
, bash
, ash
/dash
, zsh
, yash
.
var=23
: that's the correct variable assignment syntax: a word that consists of unquoted letters, digits or underscores followed by an unquoted=
that appears before a command argument (here it's on its own)var =23
, thevar
command with=23
as argument (except inzsh
where=something
is a special operator that expands to the path of thesomething
command. Here, you'd likely to get an error as23
is unlikely to be a valid command name).var= 23
: an assignmentvar=
followed by a command name23
. That's meant to execute23
withvar=
passed to its environment (var
environment variable with an empty value).var = 23
,var
command with=
and23
as argument. Try withecho = 23
for instance.
Csh family
csh
and tcsh
. Variable assignments there are with the set var = value
syntax for scalar variables, set var = (a b)
for arrays, setenv var value
for environment variables, @ var=1+1
for assignment and arithmetic evaluation.
So:
var=23
is just invoking thevar=23
command.var =23
is invoking thevar
command with=23
as argument.var= 23
is invoking thevar=
command with23
as argumentvar = 23
is invoking thevar
command with=
and23
as arguments.
Rc family
That's rc
, es
and akanga
. In those shells, variables are arrays and assignments are with var = (foo bar)
, with var = foo
being short for var = (foo)
(an array with one foo
element) and var =
short for var = ()
(array with no element, use var = ''
or var = ('')
for an array with one empty element).
In any case, blanks (space or tab) around =
are allowed and optional. So in those shells those 4 commands are equivalent and equivalent to var = (23)
to assign an array with one element being 23
.
Fish
In fish
, the variable assignment syntax is set var value1 value2
. Like in rc
, variables are arrays.
So the behaviour would be the same as with csh
, except that fish
won't let you run a command with a =
in its name. If you have such a command, you need to invoke it via sh
for instance: sh -c 'exec weird===cmd'
.
So all var=23
and var= 23
will give you an error, var =23
will call the var
command with =23
as argument and var = 23
will call the var
command with =
and 23
as arguments.
var=23
assigns 23 to the variable var
.
var =23
tries to run command (or alias, or function) var
with argument =23
var = 23
ditto, but arguments =
and 23
var= 23
sets var
environment variable to blank string, then runs command 23
Yes, shell is weird as a programming language. But it makes perfect sense as a shell for interactive use, where spaces separate commands and arguments. Most "special characters" (=
in this case) have special meaning only in particular positions, to allow for almost arbitrary arguments to commands.See the above interpretations.
var=23
is the correct syntax for assigning value to a variable.var =23
is considered as commandvar
with=23
option/argument for commandvar
(Though correct/standard syntax for argument/option is-option
or--option
)var= 23
will assigns nothing tovar
as white-space breaks the process of assignment and23
will be considered as another command. The workaround isvar=\ 23
orvar=' 23'
for storing white-space.var = 23
has similar effect as discussed in 2nd case.Actually this type usage of space around
=
is usually used in testing condition inside[[ ]]
. Example for Bash:string1 = string2 True if the strings are equal. = should be used with the test command for POSIX conformance. When used with the [[ command, this performs pattern matching as described above (Compound Commands).
And after-all the behaviour of white-space around =
depends on your shell and the programming languages.