6 SC1066
John Gardner edited this page 2021-12-22 20:05:31 +11:00

Don't use $ on the left side of assignments.

Problematic code:

$greeting="Hello World"

Correct code:

greeting="Hello World"

Alternatively, if the goal was to assign to a variable whose name is in another variable (indirection), use declare:

name=foo
declare "$name=hello world"
echo "$foo"

Or if you actually wanted to compare the value, use a test expression:

if [ "$greeting" = "hello world" ]
then
  echo "Programmer, I presume?"
fi

Rationale:

Unlike Perl or PHP, $ is not used when assigning to a variable.

Exceptions

None.