mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2298 (markdown)
50
SC2298.md
Normal file
50
SC2298.md
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
## `${$x}` is invalid. For expansion, use ${x}. For indirection, use arrays, ${!x} or (for sh) eval.
|
||||||
|
|
||||||
|
(or `${${x}}` is invalid)
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# Expecting $RETRIES or 3 if unset
|
||||||
|
retries=${$RETRIES:-3}
|
||||||
|
```
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
```sh
|
||||||
|
mypath="/tmp/foo.txt"
|
||||||
|
var=mypath
|
||||||
|
result=${$var##*/} # Expecting ${mypath##*/}, i.e. 'foo.txt'
|
||||||
|
```
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
retries=${RETRIES:-3}
|
||||||
|
```
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
```sh
|
||||||
|
mypath="/tmp/foo.txt"
|
||||||
|
var=mypath
|
||||||
|
result=${!var}
|
||||||
|
result=${result##*/}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
ShellCheck found a parameter expansion `${..}` where the first element was a second parameter expansion, either `${$x..}` or `${${x}..}`. This is not valid.
|
||||||
|
|
||||||
|
In the first example, the extra `$` was unintentional and should simply be deleted.
|
||||||
|
|
||||||
|
In the second example, `${$var##*/}` was used in the hopes that it would expand to `${myvar##*/}` and subsequently strip the path. This is not possible, and `var` must instead be expanded indirectly in a separate step, before the path can be stripped as usual.
|
||||||
|
|
||||||
|
### Exceptions:
|
||||||
|
|
||||||
|
None
|
||||||
|
|
||||||
|
### Related resources:
|
||||||
|
|
||||||
|
* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
|
Reference in New Issue
Block a user