Shell Scripting : Convert a String to Uppercase

Welcome to another shell scripting tutorial! So far, we've handled numbers, but a huge part of programming involves working with text, also known as "strings."

Today, we'll write a simple script that asks the user to enter a string in lowercase and then uses a built-in Bash feature to instantly convert it to all uppercase. This is a great way to learn about parameter expansion, a powerful tool in shell scripting.

Shell Scripting  Convert a String to Uppercase

Let's dive in!

The Shell Script (Modern Bash)

This first method is the cleanest and most efficient way to do this, but it requires a modern version of Bash (version 4.0 or newer). This is standard on almost all modern Linux systems and macOS.

#!/bin/bash

# This script converts a lowercase string to uppercase.

# 1. Ask the user for their string
read -p "Enter a string in lowercase: " user_string

# 2. Convert the string to uppercase
# This is "Parameter Expansion"
# The '^^' operator converts the entire variable to uppercase.
uppercase_string=${user_string^^}

# 3. Print the result
echo "------------------------------"
echo "Original:  $user_string"
echo "UPPERCASE: $uppercase_string"

How It Works: A Step-by-Step Breakdown

Let's look at the new, key part of this script.

  • #!/bin/bash
    The "shebang," telling the system to use the Bash shell.

  • read -p "..." user_string
    The read -p command prints the prompt ("...") and then stores whatever the user types into the user_string variable.

  • uppercase_string=${user_string^^}
    This is the magic!
    • ${...}: This syntax is called Parameter Expansion. It's a way to manipulate the value of a variable.
    • ^^: This specific operator tells Bash to take the value of the variable (user_string) and convert every character in it to its uppercase equivalent.
    • We then store this new, converted string in a variable called uppercase_string.

  • echo "..."
    Finally, we print both the original string and the new uppercase string so we can see the change.

Alternative Method: The Classic tr Command

What if you're on a very old system or using a different shell (like sh)? The ^^ operator won't work. The classic, portable way to do this is with the tr (translate) command.

Here's how you would write the script using this method:

#!/bin/bash

read -p "Enter a string in lowercase: " user_string

# Use the 'tr' command to "translate" characters
# We "echo" the string, then "pipe" it to 'tr'
uppercase_string=$(echo "$user_string" | tr 'a-z' 'A-Z')

echo "------------------------------"
echo "Original:  $user_string"
echo "UPPERCASE: $uppercase_string"

How this works:

  1. echo "$user_string": This prints the user's string.
  2. |: This is the "pipe" operator. It takes the output from the command on its left (echo) and sends it as the input to the command on its right (tr).
  3. tr 'a-z' 'A-Z': The tr (translate) command is told to find every character in the range 'a' through 'z' and replace it with the corresponding character in the range 'A' through 'Z'.
  4. $(...): This is "Command Substitution." It runs all the commands inside and captures their final output, storing it in the uppercase_string variable.

How to Run the Script

Let's run the first (modern) script.

Step 1: Create the file
Use nano to create your script file:

nano toupper.sh

Step 2: Paste the code
Copy the first script (the modern Bash version) and paste it into the nano editor.

Step 3: Save and Exit
(In nano: Press Ctrl+O, Enter to save. Press Ctrl+X to exit.)

Step 4: Make the script executable
Give the file permission to run:

chmod +x toupper.sh

Step 5: Run it!
Execute your script:

./toupper.sh

Example Output

When you run the script, it should look just like this:

$ ./toupper.sh
Enter a string in lowercase: hello world! this is a test.
------------------------------
Original:  hello world! this is a test.
UPPERCASE: HELLO WORLD! THIS IS A TEST.

Challenge Yourself!

You've just learned a powerful and easy way to manipulate strings in Bash. Now for a challenge: can you write a script that does the opposite? It should ask for an uppercase string and convert it to lowercase.

Hint: The modern operator for this is ,, (e.g., ${variable,,}). How would you do it with tr?

Happy scripting!

Post a Comment

0 Comments