mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Created SC2020 (markdown)
21
SC2020.md
Normal file
21
SC2020.md
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
# tr replaces sets of chars, not words (mentioned due to duplicates).
|
||||||
|
|
||||||
|
### Problematic code:
|
||||||
|
|
||||||
|
echo 'hello world' | tr 'hello' 'goodbye'
|
||||||
|
|
||||||
|
### Correct code:
|
||||||
|
|
||||||
|
echo 'hello world' | sed -e 's/hello/goodbye'
|
||||||
|
|
||||||
|
### Rationale:
|
||||||
|
|
||||||
|
`tr` is for `tr`ansliteration, turning some characters into other characters. It doesn't match strings or words, only individual characters.
|
||||||
|
|
||||||
|
In this case, it transliterates h->g, e->o, l->d, o->y, resulting in the string "goddb wbrdd" instead of "goodbye world".
|
||||||
|
|
||||||
|
The solution is to use a tool that does string search and replace, such as sed.
|
||||||
|
|
||||||
|
### Contraindications
|
||||||
|
|
||||||
|
None.
|
Reference in New Issue
Block a user