Created SC2281 (markdown)

Vidar Holen
2020-12-30 20:17:03 -08:00
parent 8318e0ea2f
commit f3ee017cea

39
SC2281.md Normal file

@@ -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