Bird
Raised Fist0
Linux CLIscripting~20 mins

tr (translate characters) in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
tr Command Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this tr command?
Given the input string hello world, what is the output of the command echo 'hello world' | tr 'a-z' 'A-Z'?
Linux CLI
echo 'hello world' | tr 'a-z' 'A-Z'
Ahello world
BHELLO WORLD
CHELLO world
DError: invalid range
Attempts:
2 left
💡 Hint
The tr command can convert lowercase letters to uppercase by specifying ranges.
💻 Command Output
intermediate
1:30remaining
What does this tr command output?
What is the output of echo '123-456-7890' | tr -d '-'?
Linux CLI
echo '123-456-7890' | tr -d '-'
A1234567890
B123 456 7890
CError: invalid option
D123-456-7890
Attempts:
2 left
💡 Hint
The -d option deletes characters specified in the set.
💻 Command Output
advanced
1:30remaining
What is the output of this tr command with squeeze?
Given the input echo 'aaabbbcccaaa' | tr -s 'a', what is the output?
Linux CLI
echo 'aaabbbcccaaa' | tr -s 'a'
Aabbbccc
Babbbcccaa
Cabbbccca
DError: invalid option
Attempts:
2 left
💡 Hint
The -s option squeezes repeated characters in the set into one.
💻 Command Output
advanced
1:30remaining
What error does this tr command produce?
What error message results from running echo 'test' | tr 'a-z' 'A-Z' '0-9'?
Linux CLI
echo 'test' | tr 'a-z' 'A-Z' '0-9'
Atr: extra operand '0-9'
Btest
CTEST
DError: invalid range
Attempts:
2 left
💡 Hint
The tr command accepts only two sets of characters for translation.
🚀 Application
expert
2:00remaining
How many characters are in the output after this tr command?
Given the input string echo 'aabbccddeeff' | tr -d 'bdf', how many characters does the output contain?
Linux CLI
echo 'aabbccddeeff' | tr -d 'bdf'
A8
B9
C12
D6
Attempts:
2 left
💡 Hint
Count the characters after deleting all 'b', 'd', and 'f'.

Practice

(1/5)
1. What does the tr command do in Linux?
easy
A. It translates or deletes characters from input text.
B. It lists files in a directory.
C. It compiles source code.
D. It displays the current directory path.

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 A, C, and D describe other commands like ls, compilers, and pwd, which are unrelated to tr.
  3. Final Answer:

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

    tr = translate/delete chars [OK]
Hint: Remember: tr = translate characters [OK]
Common Mistakes:
  • Confusing tr with ls or pwd commands
  • Thinking tr compiles code
  • Assuming tr lists files
2. Which of the following is the correct syntax to replace all lowercase 'a' characters with 'b' using tr?
easy
A. tr -replace a b
B. tr a-z b
C. tr 'a' 'b'
D. tr 'a' to 'b'

Solution

  1. Step 1: Recall tr syntax

    The correct syntax uses single quotes around the sets of characters: tr 'a' 'b'.
  2. Step 2: Evaluate options

    tr a-z b uses a range which translates all lowercase letters to 'b' (padded), incorrect for only 'a'. Options A and C use invalid syntax for tr.
  3. Final Answer:

    tr 'a' 'b' -> Option C
  4. Quick Check:

    Use quotes around sets in tr [OK]
Hint: Use quotes around characters in tr command [OK]
Common Mistakes:
  • Using range 'a-z' for single character
  • Using invalid options like -replace
  • Adding extra words like 'to'
3. What is the output of the command:
echo "hello world" | tr 'a-z' 'A-Z'?
medium
A. HELLO WORLD
B. hello world
C. HELLO world
D. Error: invalid range

Solution

  1. Step 1: Understand the command

    The command converts all lowercase letters (a-z) to uppercase (A-Z) in the input text "hello world".
  2. Step 2: Apply translation

    All letters become uppercase, so "hello world" becomes "HELLO WORLD".
  3. Final Answer:

    HELLO WORLD -> Option A
  4. Quick Check:

    Lowercase to uppercase translation = HELLO WORLD [OK]
Hint: tr 'a-z' 'A-Z' converts lowercase to uppercase [OK]
Common Mistakes:
  • Expecting partial uppercase
  • Confusing input and output
  • Thinking it causes an error
4. Identify the error in this command:
echo "123abc" | tr '0-9' 'a-j'
medium
A. Ranges in sets are mismatched length causing error
B. No error, command works correctly
C. Output will be empty
D. Quotes are missing around sets

Solution

  1. Step 1: Analyze the command

    The command replaces digits 0-9 with letters a-j. Both sets have equal length (10 characters).
  2. Step 2: Check for syntax or runtime errors

    Quotes are present, ranges are valid and equal length, so no error occurs.
  3. Final Answer:

    No error, command works correctly -> Option B
  4. Quick Check:

    Equal length ranges with quotes = valid command [OK]
Hint: Ensure equal length sets in tr to avoid errors [OK]
Common Mistakes:
  • Assuming mismatched lengths cause errors here
  • Missing quotes around sets
  • Expecting output to be empty
5. You want to remove all vowels (a, e, i, o, u) from a text file named input.txt. Which command correctly does this using tr?
hard
A. tr -remove 'aeiou' < input.txt
B. tr 'aeiou' < input.txt
C. tr -r 'aeiou' < input.txt
D. tr -d 'aeiou' < input.txt

Solution

  1. Step 1: Understand the goal

    The goal is to delete vowels from the input text file.
  2. Step 2: Identify correct tr option

    The -d option deletes specified characters. So tr -d 'aeiou' removes vowels.
  3. Step 3: Check other options

    tr 'aeiou' < input.txt lacks the second character set, causing an error. Options C and D use invalid options.
  4. Final Answer:

    tr -d 'aeiou' < input.txt -> Option D
  5. Quick Check:

    Use -d to delete chars in tr [OK]
Hint: Use tr -d to delete characters from input [OK]
Common Mistakes:
  • Missing second character set operand
  • Using invalid options like -r or -remove
  • Forgetting to redirect input
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