12

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?

  • In most programming languages this would make no difference at all... Is there actually a difference to begin with? Also you're talking oldschool sh here? not ash/zsh/bash or other new things? – Cestarian Jan 30 '16 at 13:27
  • Only the first examples sets a variable. – DisplayName Jan 30 '16 at 13:59
  • Then don't the last 3 just throw a syntax error? or do they actually hold meaning? – Cestarian Jan 30 '16 at 14:03
  • 1
    @Cestarian they can mean something, if you have a command called var then var =23 would be pass =23 to var, and var = 23 would pass = and 23 to var. Or, if you have a command called var= then var= 23 would pass 23 to the command var=. – DisplayName Jan 30 '16 at 14:20
  • 1
    @Cestarian Replaced by bash, which does exactly the same thing as sh in all four cases. – pfnuesel Jan 30 '16 at 14:25
9

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 followed by an unquoted = that appears before a command argument (here it's on its own)
  • var =23, the var command with =23 as argument (except in zsh where =something is a special operator that expands to the path of the something command. Here, you'd likely to get an error as 23 is unlikely to be a valid command name).
  • var= 23: an assignment var= followed by a command name 23. That's meant to execute 23 with var= passed to its environment (var environment variable with an empty value).
  • var = 23, var command with = and 23 as argument. Try with echo = 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 the var=23 command.
  • var =23 is invoking the var command with =23 as argument.
  • var= 23 is invoking the var= command with 23 as argument
  • var = 23 is invoking the var command with = and 23 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 = '' 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 env for instance: env 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.

8

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.

3
  • var=23 is the correct syntax for assigning value to a variable.
  • var =23 is considered as command var with =23 option/argument for command var (Though correct/standard syntax for argument/option is -option or --option)
  • var= 23 will assigns nothing to var as white-space breaks the process of assignment and 23 will be considered as another command. The workaround is var=\ 23 or 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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