What is the difference between below variables assignments?
var=23 var =23 var= 23 var = 23
Is there any difference in space around the assignment operator?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
That very much depends on the shell. If we only look at the 4 main shell families (Bourne, csh, rc, fish):
That is the Bourne shell and all its variants and
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, the
=23as argument (except in
=somethingis a special operator that expands to the path of the
somethingcommand. Here, you'd likely to get an error as
23is unlikely to be a valid command name).
var= 23: an assignment
var=followed by a command name
23. That's meant to execute
var=passed to its environment (
varenvironment variable with an empty value).
var = 23,
23as argument. Try with
echo = 23for instance.
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.
var=23is just invoking the
var =23is invoking the
var= 23is invoking the
var = 23is invoking the
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
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'.
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
23 as arguments.
var=23 assigns 23 to the variable
var =23 tries to run command (or alias, or function)
var with argument
var = 23 ditto, but arguments
var= 23 sets
var environment variable to blank string, then runs command
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=23is the correct syntax for assigning value to a variable.
var =23is considered as command
=23option/argument for command
var(Though correct/standard syntax for argument/option is
var= 23will assigns nothing to
varas white-space breaks the process of assignment and
23will be considered as another command. The workaround is
var=' 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.