mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2028 (markdown)
25
SC2028.md
Normal file
25
SC2028.md
Normal file
@@ -0,0 +1,25 @@
|
||||
# echo won't expand escape sequences. Consider printf.
|
||||
|
||||
### Problematic code:
|
||||
|
||||
echo "Name:\t$value"
|
||||
|
||||
### Correct code:
|
||||
|
||||
printf "Name:\t%s\n" "$value"
|
||||
|
||||
### Rationale:
|
||||
|
||||
Backslash escapes like `\t` and `\n` are not expanded by echo, and become literal backslash-t, backslash-n.
|
||||
|
||||
`printf` does expand these sequences, and should be used instead.
|
||||
|
||||
Other, non-portable methods include `echo -e '\t'` and `echo $'\t'`. ShellCheck will warn if this is used in a script with shebang `#!/bin/sh`.
|
||||
|
||||
If you actually wanted a literal backslash-t, use
|
||||
|
||||
echo "\\t"
|
||||
|
||||
### Contraindications
|
||||
|
||||
None
|
Reference in New Issue
Block a user