Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!
The `tr` command is used to translate or delete characters in a string. By using the `-d` option, we can delete all lowercase letters from a string, effectively converting it to uppercase. Here’s an example:
echo "hello world" | tr '[:lower:]' '[:upper:]'
This command will output “HELLO WORLD”. Let’s break it down:
– `echo “hello world”` prints the string “hello world” to the console.
– `|` is a pipe, which takes the output of the previous command and passes it as input to the next command.
– `tr ‘[:lower:]’ ‘[:upper:]’` translates all lowercase letters to uppercase letters.
You can also use this method to convert a variable to uppercase:
myString=$(echo $myString | tr '[:lower:]' '[:upper:]')
This will output “HELLO WORLD”. Here’s what’s happening:
– `myString=”hello world”` sets the variable `myString` to the string “hello world”.
– `myString=$(echo $myString | tr ‘[:lower:]’ ‘[:upper:]’)` sets `myString` to the output of the `echo` command piped into the `tr` command, which converts all lowercase letters to uppercase letters.
– `echo $myString` prints the value of `myString` to the console.