mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2088 (markdown)
32
SC2088.md
Normal file
32
SC2088.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
## Note that ~ doesn't expand in quotes.
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
rm "~/Desktop/$filename"
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
rm ~/"Desktop/$filename"
|
||||||
|
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
Tilde does not expand to the user's home directory when it's double quoted.
|
||||||
|
|
||||||
|
To expand it, the tilde, the optional username, and its following slash should be outside of the double quotes.
|
||||||
|
|
||||||
|
These strings expand:
|
||||||
|
|
||||||
|
~/file # Correct: tilde up to the slash is unquoted (as is everything else)
|
||||||
|
~/"$var" # Correct: tilde up to the slash is unquoted
|
||||||
|
~user/"hello world.txt" # Correct: tilde up to the slash is unquoted
|
||||||
|
|
||||||
|
These strings do not, leaving a literal tilde:
|
||||||
|
|
||||||
|
# Fails!
|
||||||
|
"~/file" # Wrong: tilde and slash are quoted when they shouldn't be.
|
||||||
|
~"/file" # Wrong: slash is quoted when it shouldn't be.
|
||||||
|
~user"/hello world.txt" # Wrong: slash is quoted when it shouldn't be.
|
||||||
|
|
||||||
|
### Contraindications
|
||||||
|
|
||||||
|
If you don't want the tilde to be expanded, you can ignore this message.
|
Reference in New Issue
Block a user