In this article, we will show you how to convert a string to uppercase or lowercase in Bash.
1. Convert to Uppercase or Lowercase
- If you append the keyword
^^
to the right of a string, it will be converted to uppercase. - If you append the keyword
,,
to the right of a string, it will be converted to lowercase.
This is an example of how to convert a string to uppercase or lowercase.
#!/bin/bash
str="ApPlE"
uppercase=${str^^}
lowercase=${str,,}
echo "Uppercase: ${uppercase}"
echo "Lowercase: ${lowercase}"
Output:
$ bash example.sh
Uppercase: APPLE
Lowercase: apple
2. Converting only certain characters to uppercase and lowercase
For example, if you input any character after the keyword ^^
like ^^a
, the character will be converted to uppercase in the string. The same applies to ,,A
, where A is converted to lowercase in the string.
#!/bin/bash
str="apple"
echo ${str^^p}
str2="APPLE"
echo ${str2,,E}
Output:
$ bash example.sh
aPPle
APPLe
3. Combining with other strings
This is an example of combining a string converted to uppercase and lowercase with another string.
#!/bin/bash
str="ApPlE"
str2="Juice"
echo "${str^^} ${str2}"
echo "${str,,} ${str2}"
Output:
$ bash example.sh
APPLE Juice
apple Juice
Related Posts
- How to measure elapsed time in Bash
- Ceil, Floor, Round off a devided number in Bash
- Check if a number is Positive or Negative in Bash
- How to Print a Variable in Bash
- Number Comparison Operators in Bash
- Convert between Uppercase and Lowercase in Bash
- Check if a File or Directory exists in Bash
- How to extract substring in Bash
- Bash Case Statement with examples
- How to use Command Line Arguments in Bash
- How to use Arrays in Bash
- How to Compare strings in Bash Shell
- if..else statement in Bash
- Add, Subtract, Multiply, Division with Numbers in Bash
- Bash For, Whlie Loop Examples
- How to Pass and Use command line arguments in Bash