Skip to content
koalaman edited this page Feb 10, 2014 · 5 revisions

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 transliteration, 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.

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally