mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2281 (markdown)
39
SC2281.md
Normal file
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
|
Reference in New Issue
Block a user