0
0
Bash Scriptingscripting~10 mins

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

Choose your learning style9 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.