Skip to main content
added 2 characters in body
Source Link
Kamil Maciorowski
  • 75.7k
  • 22
  • 152
  • 229

The syntax to assign a value to a variable is foo=bar, not foo = bar. Whitespaces matter. The latter syntax is a command foo with arguments = and bar.

Few examples of how = is interpreted:

code meaning
foo=bar proper assignment; now the value of foo is bar
foo = bar command foo with arguments = and bar
foo =bar command foo with one argument =bar
foo= bar command bar with foo in its environment; the value of foo is empty
foo=1 bar command bar with foo in its environment; the value of foo is 1
foo='1 bar' proper assignment; now the value of foo is 1 bar
foo=' bar' proper assignment; now the value of foo is bar (note the leading space)
foo=\ bar proper assignment; now the value of foo is bar (note the leading space)
foo-x=bar command foo-x=bar because (because foo-x is not a valid name for a shell variable)

This is not specific to Zsh. The POSIX shell (sh) and POSIX-compliant shells behave this way. Zsh (while not being POSIX-compliant in general) also follows.

The syntax to assign a value to a variable is foo=bar, not foo = bar. Whitespaces matter. The latter syntax is a command foo with arguments = and bar.

Few examples of how = is interpreted:

code meaning
foo=bar proper assignment; now the value of foo is bar
foo = bar command foo with arguments = and bar
foo =bar command foo with one argument =bar
foo= bar command bar with foo in its environment; the value of foo is empty
foo=1 bar command bar with foo in its environment; the value of foo is 1
foo='1 bar' proper assignment; now the value of foo is 1 bar
foo=' bar' proper assignment; now the value of foo is bar (note the leading space)
foo=\ bar proper assignment; now the value of foo is bar (note the leading space)
foo-x=bar command foo-x=bar because foo-x is not a valid name for a shell variable

This is not specific to Zsh. The POSIX shell (sh) and POSIX-compliant shells behave this way. Zsh (while not being POSIX-compliant in general) also follows.

The syntax to assign a value to a variable is foo=bar, not foo = bar. Whitespaces matter. The latter syntax is a command foo with arguments = and bar.

Few examples of how = is interpreted:

code meaning
foo=bar proper assignment; now the value of foo is bar
foo = bar command foo with arguments = and bar
foo =bar command foo with one argument =bar
foo= bar command bar with foo in its environment; the value of foo is empty
foo=1 bar command bar with foo in its environment; the value of foo is 1
foo='1 bar' proper assignment; now the value of foo is 1 bar
foo=' bar' proper assignment; now the value of foo is bar (note the leading space)
foo=\ bar proper assignment; now the value of foo is bar (note the leading space)
foo-x=bar command foo-x=bar (because foo-x is not a valid name for a shell variable)

This is not specific to Zsh. The POSIX shell (sh) and POSIX-compliant shells behave this way. Zsh (while not being POSIX-compliant in general) also follows.

added 470 characters in body; added 26 characters in body; added 94 characters in body
Source Link
Kamil Maciorowski
  • 75.7k
  • 22
  • 152
  • 229

The syntax to assign a value to a variable is foo=bar, not foo = bar. Whitespaces matter. The latter syntax is a command foo with arguments = and bar.

Few examples of how = is interpreted:

code meaning
foo=bar proper assignment; now the value of foo is bar
foo = bar command foo with arguments = and bar
foo =bar command foo with one argument =bar
foo= bar command bar with foo in its environment; the value of foo is empty
foo=1 bar command bar with foo in its environment; the value of foo is 1
foo='1 bar' proper assignment; now the value of foo is 1 bar
foo=' bar' proper assignment; now the value of foo is bar (note the leading space)
foo=\ bar proper assignment; now the value of foo is bar (note the leading space)
foo-x=barcommand foo-x=bar because foo-x is not a valid name for a shell variable

This is not specific to Zsh. The POSIX shell (sh) and POSIX-compliant shells behave this way. Zsh (while not being POSIX-compliant in general) also follows.

The syntax to assign a value to a variable is foo=bar, not foo = bar. Whitespaces matter. The latter syntax is a command foo with arguments = and bar.

Few examples of how = is interpreted:

code meaning
foo=bar proper assignment; now the value of foo is bar
foo = bar command foo with arguments = and bar
foo =bar command foo with one argument =bar
foo= bar command bar with foo in its environment; the value of foo is empty
foo=1 bar command bar with foo in its environment; the value of foo is 1
foo='1 bar' proper assignment; now the value of foo is 1 bar
foo=' bar' proper assignment; now the value of foo is bar
foo=\ bar proper assignment; now the value of foo is bar

This is not specific to Zsh. The POSIX shell (sh) and POSIX-compliant shells behave this way. Zsh (while not being POSIX-compliant in general) also follows.

The syntax to assign a value to a variable is foo=bar, not foo = bar. Whitespaces matter. The latter syntax is a command foo with arguments = and bar.

Few examples of how = is interpreted:

code meaning
foo=bar proper assignment; now the value of foo is bar
foo = bar command foo with arguments = and bar
foo =bar command foo with one argument =bar
foo= bar command bar with foo in its environment; the value of foo is empty
foo=1 bar command bar with foo in its environment; the value of foo is 1
foo='1 bar' proper assignment; now the value of foo is 1 bar
foo=' bar' proper assignment; now the value of foo is bar (note the leading space)
foo=\ bar proper assignment; now the value of foo is bar (note the leading space)
foo-x=barcommand foo-x=bar because foo-x is not a valid name for a shell variable

This is not specific to Zsh. The POSIX shell (sh) and POSIX-compliant shells behave this way. Zsh (while not being POSIX-compliant in general) also follows.

added 470 characters in body; added 26 characters in body
Source Link
Kamil Maciorowski
  • 75.7k
  • 22
  • 152
  • 229

The syntax to assign a value to a variable is foo=bar, not foo = bar. Whitespaces matter.

  The latter syntax is a command foo with arguments = and bar.

Few examples of how = is interpreted:

  • foo=bar – proper assignment; the value of foo is bar
  • foo = bar – command foo with arguments = and bar
  • foo =bar – command foo with one argument =bar
  • foo= bar – command bar with foo in its environment; the value of foo is empty
  • foo=1 bar – command bar with foo in its environment; the value of foo is 1
  • foo='1 bar' – proper assignment; the value of foo is 1 bar
codemeaning
foo=barproper assignment; now the value of foo is bar
foo = barcommand foo with arguments = and bar
foo =barcommand foo with one argument =bar
foo= barcommand bar with foo in its environment; the value of foo is empty
foo=1 barcommand bar with foo in its environment; the value of foo is 1
foo='1 bar'proper assignment; now the value of foo is 1 bar
foo=' bar'proper assignment; now the value of foo is bar
foo=\ barproper assignment; now the value of foo is bar

This is not specific to Zsh. The POSIX shell (sh) and POSIX-compliant shells behave this way. Zsh (while not being POSIX-compliant in general) also follows.

The syntax to assign a value to a variable is foo=bar, not foo = bar. Whitespaces matter.

  The latter syntax is a command foo with arguments = and bar.

Few examples of how = is interpreted:

  • foo=bar – proper assignment; the value of foo is bar
  • foo = bar – command foo with arguments = and bar
  • foo =bar – command foo with one argument =bar
  • foo= bar – command bar with foo in its environment; the value of foo is empty
  • foo=1 bar – command bar with foo in its environment; the value of foo is 1
  • foo='1 bar' – proper assignment; the value of foo is 1 bar

The syntax to assign a value to a variable is foo=bar, not foo = bar. Whitespaces matter. The latter syntax is a command foo with arguments = and bar.

Few examples of how = is interpreted:

codemeaning
foo=barproper assignment; now the value of foo is bar
foo = barcommand foo with arguments = and bar
foo =barcommand foo with one argument =bar
foo= barcommand bar with foo in its environment; the value of foo is empty
foo=1 barcommand bar with foo in its environment; the value of foo is 1
foo='1 bar'proper assignment; now the value of foo is 1 bar
foo=' bar'proper assignment; now the value of foo is bar
foo=\ barproper assignment; now the value of foo is bar

This is not specific to Zsh. The POSIX shell (sh) and POSIX-compliant shells behave this way. Zsh (while not being POSIX-compliant in general) also follows.

added 470 characters in body
Source Link
Kamil Maciorowski
  • 75.7k
  • 22
  • 152
  • 229
Loading
Source Link
Kamil Maciorowski
  • 75.7k
  • 22
  • 152
  • 229
Loading