mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2299 (markdown)
32
SC2299.md
Normal file
32
SC2299.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
## Parameter expansions can't be nested. Use temporary variables.
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
path="/path/to/MyFile.mp3"
|
||||||
|
echo "Playing ${${path##*/}%.*}" # Expect: Playing MyFile
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
path="/path/to/MyFile.mp3"
|
||||||
|
tmp=${path##*/}
|
||||||
|
echo "Playing ${tmp%.*}"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
ShellCheck found what appears to be a nested parameter expansion. In the example, it was hoping to combine `${var##*/}` to strip the directory and `${var%.*}` to strip the extension.
|
||||||
|
|
||||||
|
Parameter expansions can't be nested. Use temporary variables instead, so that each parameter expansion only does a single operation.
|
||||||
|
|
||||||
|
Alternatively, if the goal is to dynamically generate and expand a variable name, see [[SC2082]].
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
None
|
||||||
|
|
||||||
|
### Related resources:
|
||||||
|
|
||||||
|
* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
|
Reference in New Issue
Block a user