diff --git a/SC2281.md b/SC2281.md new file mode 100644 index 0000000..a345e82 --- /dev/null +++ b/SC2281.md @@ -0,0 +1,39 @@ +## Don't use $/${} on the left side of assignments. + +### Problematic code: + +```sh +$greeting="Hello World" +${greeting}="Hello World" +``` + +### Correct code: + +```sh +greeting="Hello World" +``` + +Alternatively, if the goal was to assign to a variable whose name is in another variable (indirection), use `declare`: + +```sh +name=foo +declare "$name=hello world" +echo "$foo" +``` + +Or if you actually wanted to compare the value, use a test expression: + +```sh +if [ "$greeting" = "hello world" ] +then + echo "Programmer, I presume?" +fi +``` + +### Rationale: + +Unlike Perl or PHP, `$` is not used on the left-hand side of `=` when assigning to a variable. + +### Exceptions + +If you wanted to