Bird
Raised Fist0
Bash Scriptingscripting~10 mins

tr for character transformation in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - tr for character transformation
Input string
↓
tr command reads input
↓
Map each character from set1 to set2
↓
Output transformed string
The tr command reads input, replaces characters from one set with another, then outputs the transformed string.
Execution Sample
Bash Scripting
echo "hello world" | tr 'a-z' 'A-Z'
Transforms all lowercase letters in 'hello world' to uppercase.
Execution Table
StepInput CharacterTransformation RuleOutput Character
1hh -> HH
2ee -> EE
3ll -> LL
4ll -> LL
5oo -> OO
6 space unchanged
7ww -> WW
8oo -> OO
9rr -> RR
10ll -> LL
11dd -> DD
💡 All characters processed, input fully transformed.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8After 9After 10After 11
input_charhello world
output_charHELLO WORLD
Key Moments - 3 Insights
Why does the space character remain unchanged?
Because the space is not in the 'a-z' range specified for transformation, so tr leaves it as is (see execution_table step 6).
What happens if the input has characters outside 'a-z'?
Characters not in the first set are passed through unchanged, as shown by the space character in step 6.
Why do multiple 'l' characters each transform separately?
tr processes each character one by one, so repeated characters are transformed individually (see steps 3,4,10).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output character at step 5?
AL
Bo
CO
DE
💡 Hint
Check the 'Output Character' column at step 5 in the execution_table.
At which step does the input character remain unchanged?
AStep 11
BStep 6
CStep 1
DStep 3
💡 Hint
Look for a step where 'Transformation Rule' says 'space unchanged' in the execution_table.
If the input was 'HELLO', what would be the output using the same tr command?
AHELLO (unchanged)
Bhello
CHELLO
DError
💡 Hint
Since 'H' is not in 'a-z', tr does not transform uppercase letters (see key_moments about characters outside the set).
Concept Snapshot
tr command replaces characters from one set with another.
Syntax: tr 'set1' 'set2'
Reads input, transforms each character in set1 to corresponding in set2.
Characters not in set1 remain unchanged.
Common use: change case, delete chars.
Example: echo "abc" | tr 'a-c' 'x-z' outputs "xyz".
Full Transcript
The tr command in bash reads input text and replaces characters from one set with characters from another set. For example, echo "hello world" | tr 'a-z' 'A-Z' converts all lowercase letters to uppercase. Each character is processed one by one. Characters not in the first set, like spaces, remain unchanged. This makes tr useful for simple character transformations like changing case or replacing specific letters. The execution table shows each input character, the rule applied, and the output character. This step-by-step trace helps beginners see exactly how tr works.

Practice

(1/5)
1. What does the tr command do in bash scripting?
easy
A. It transforms or deletes characters from input text streams.
B. It compiles bash scripts into executables.
C. It lists files in a directory.
D. It runs commands in the background.

Solution

  1. Step 1: Understand the purpose of tr

    The tr command is used to translate or delete characters from input text streams.
  2. Step 2: Compare with other options

    Options B, C, and D describe unrelated commands or functions in bash.
  3. Final Answer:

    It transforms or deletes characters from input text streams. -> Option A
  4. Quick Check:

    tr = transform/delete characters [OK]
Hint: Remember: tr means translate characters [OK]
Common Mistakes:
  • Confusing tr with file listing commands like ls
  • Thinking tr compiles scripts
  • Assuming tr runs commands in background
2. Which of the following is the correct syntax to convert lowercase letters to uppercase using tr?
easy
A. echo "hello" | tr 'A-Z' '0-9'
B. echo "hello" | tr 'a-z' 'A-Z'
C. echo "hello" | tr '0-9' 'A-Z'
D. echo "hello" | tr 'A-Z' 'a-z'

Solution

  1. Step 1: Identify the correct character ranges for case conversion

    To convert lowercase to uppercase, use 'a-z' as source and 'A-Z' as target.
  2. Step 2: Check the syntax correctness

    echo "hello" | tr 'a-z' 'A-Z' correctly uses tr 'a-z' 'A-Z'. Other options either reverse case or use wrong ranges.
  3. Final Answer:

    echo "hello" | tr 'a-z' 'A-Z' -> Option B
  4. Quick Check:

    Lowercase to uppercase = echo "hello" | tr 'a-z' 'A-Z' [OK]
Hint: Use 'a-z' then 'A-Z' to uppercase letters [OK]
Common Mistakes:
  • Swapping source and target ranges
  • Using digits instead of letters
  • Mixing uppercase and lowercase incorrectly
3. What is the output of this command?
echo "Hello World 123" | tr -d '0-9'
medium
A. Hello World 123
B. HelloWorld123
C. HelloWorld
D. Hello World

Solution

  1. Step 1: Understand the -d option in tr

    The -d option deletes characters matching the set given, here digits '0-9'.
  2. Step 2: Apply deletion to the input string

    All digits '1', '2', '3' are removed, leaving "Hello World " with spaces intact.
  3. Final Answer:

    Hello World -> Option D
  4. Quick Check:

    Digits deleted = "Hello World " [OK]
Hint: Use -d to delete characters, here digits removed [OK]
Common Mistakes:
  • Expecting digits to be replaced, not deleted
  • Removing spaces accidentally
  • Confusing -d with character translation
4. Identify the error in this command:
echo "abc123" | tr 'a-z' 'A-Z' -d '0-9'
medium
A. The command will convert letters but ignore digits.
B. The order of options is wrong; -d must come before sets.
C. You cannot combine translation and deletion in one tr command.
D. The syntax is correct and will work as expected.

Solution

  1. Step 1: Understand tr option usage

    tr does not support combining translation and deletion in one command; they are separate operations.
  2. Step 2: Analyze the command

    The command tries to translate and delete simultaneously, which is invalid syntax.
  3. Final Answer:

    You cannot combine translation and deletion in one tr command. -> Option C
  4. Quick Check:

    Translation + deletion together = invalid [OK]
Hint: tr does either translate OR delete, not both at once [OK]
Common Mistakes:
  • Trying to chain translate and delete in one tr call
  • Assuming option order fixes the problem
  • Expecting partial command success
5. You want to replace all vowels in a string with the character '*', but keep other characters unchanged. Which command achieves this?
hard
A. echo "banana" | tr 'aeiou' '*'
B. echo "banana" | tr 'aeiou' 'AEIOU'
C. echo "banana" | tr -d 'aeiou'
D. echo "banana" | tr 'aeiou' 'ABCDE'

Solution

  1. Step 1: Understand character replacement with tr

    To replace vowels with '*', map all vowels to a single character '*'.
  2. Step 2: Analyze each option

    echo "banana" | tr 'aeiou' '*' replaces each vowel with '*'. echo "banana" | tr 'aeiou' 'AEIOU' changes vowels to uppercase vowels. echo "banana" | tr -d 'aeiou' deletes vowels. echo "banana" | tr 'aeiou' 'ABCDE' replaces vowels with A,B,C,D,E respectively.
  3. Final Answer:

    echo "banana" | tr 'aeiou' '*' -> Option A
  4. Quick Check:

    Vowels replaced by '*' = echo "banana" | tr 'aeiou' '*' [OK]
Hint: Map vowels to '*' with single char target in tr [OK]
Common Mistakes:
  • Using a target set like 'ABCDE' instead of '*'
  • Deleting vowels instead of replacing
  • Replacing vowels with uppercase instead of '*'
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard