mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Updated SC2097 (markdown)
@@ -9,15 +9,15 @@ name=World cmd -m "Hello $name"
|
||||
### Correct code:
|
||||
|
||||
```sh
|
||||
export name=World
|
||||
name=World
|
||||
cmd -m "Hello $name"
|
||||
```
|
||||
|
||||
To prevent setting the variable, this can also be done in a subshell:
|
||||
If the original goal was to limit the scope of the variable, this can also be done in a subshell:
|
||||
|
||||
```sh
|
||||
(
|
||||
export name=World
|
||||
name=World
|
||||
cmd -m "Hello $name"
|
||||
) # 'name' does not leave this subshell
|
||||
```
|
||||
@@ -28,7 +28,7 @@ In `name=World cmd "$name"`, `name=World` is passed in as part of the environmen
|
||||
|
||||
However, `"$name"` is not expanded by `cmd`. `"$name"` is expanded by the shell before `cmd` is ever executed, and thus it will not use the new value.
|
||||
|
||||
The solution is to set the variable and export the variable first. If limited scope is desired, a `( subshell )` can be used.
|
||||
The solution is to set the variable first, then use it as a parameter. If limited scope is desired, a `( subshell )` can be used.
|
||||
|
||||
### Exceptions
|
||||
|
||||
|
Reference in New Issue
Block a user