📖 Scenario: You work in a small office where you often receive text files with inconsistent letter cases. Your task is to automate the process of converting all lowercase letters to uppercase to maintain uniformity in reports.
🎯 Goal: Build a simple Bash script that uses the tr command to convert lowercase letters in a given string to uppercase.
📋 What You'll Learn
Create a variable called input_text with the exact value hello world
Create a variable called uppercase_text that uses tr to convert input_text from lowercase to uppercase
Print the value of uppercase_text
💡 Why This Matters
🌍 Real World
Automating text formatting tasks like converting letter cases in reports or filenames.
💼 Career
Shell scripting skills are essential for system administrators and developers to automate repetitive text processing tasks efficiently.
Progress0 / 4 steps
1
DATA SETUP: Create the input text variable
Create a variable called input_text and set it to the string hello world exactly.
Bash Scripting
Hint
Use input_text="hello world" to assign the string.
2
CONFIGURATION: Prepare to transform characters
Create a variable called uppercase_text that uses the tr command to convert all lowercase letters in input_text to uppercase.
Bash Scripting
Hint
Use $(echo "$input_text" | tr '[:lower:]' '[:upper:]') to convert lowercase to uppercase.
3
CORE LOGIC: Apply the character transformation
Use the tr command inside a command substitution to convert input_text to uppercase and assign it to uppercase_text.
Bash Scripting
Hint
This step repeats the transformation to reinforce the core logic.
4
OUTPUT: Display the transformed text
Print the value of uppercase_text to show the converted uppercase string.
Bash Scripting
Hint
Use echo "$uppercase_text" to print the result.
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
Step 1: Understand the purpose of tr
The tr command is used to translate or delete characters from input text streams.
Step 2: Compare with other options
Options B, C, and D describe unrelated commands or functions in bash.
Final Answer:
It transforms or deletes characters from input text streams. -> Option A
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
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.
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.