mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2300 (markdown)
29
SC2300.md
Normal file
29
SC2300.md
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
## Parameter expansion can't be applied to command substitutions. Use temporary variables.
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
echo "Building ${$(git rev-parse --show-toplevel)##*/}"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
tmp=$(git rev-parse --show-toplevel)
|
||||||
|
echo "Building ${tmp##*/}"
|
||||||
|
```
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
ShellCheck found a parameter expansion that begins with a command substitution, such as `$(..)` or `` `..` ``. This is not valid. Parameter expansion only works on variables (normal or special).
|
||||||
|
|
||||||
|
In the example, the user hoped to apply the construct `${var##*/}`, stripping the path, to the current git root directory as output by `git rev-parse --show-toplevel`. Since parameter expansion only works on variable, the command substitution must be assigned to a variable first like in the correct example.
|
||||||
|
|
||||||
|
If the goal was instead to dynamically generate a variable name to expand, see [[SC2082]].
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
None
|
||||||
|
|
||||||
|
### Related resources:
|
||||||
|
|
||||||
|
* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
|
Reference in New Issue
Block a user