Shell Scripting: Convert Uppercase to Lowercase
Q. How do I convert uppercase words to lowercase or vise versa under BASH shell? I've a small shell script and I'd like to convert all incoming user input to lowercase using a shell script.
A. Use tr command to convert all incoming text / words / variable data from upper to lower case or vise versa (translate all uppercase characters to lowercase).
Convert all text file data from upper to lowercase:
Type the following command at shell:
$ tr '[:upper:]' '[:lower:]' < input.txt > output.txt
Convert variable data from upper to lowercase:
$ echo $VAR_NAME | tr '[:upper:]' '[:lower:]'
$ echo $VAR_NAME | tr '[:lower:]' '[:upper:]'
Recommended readings:
=> See tr command shell scripting example for more information.
Sample Shell Script
#!/bin/bash # get filename echo -n "Enter File Name : " read fileName # make sure file exits for reading if [ ! -f $fileName ]; then echo "Filename $fileName does not exists" exit 1 fi # convert uppercase to lowercase using tr command tr '[A-Z]' '[a-z]' < $fileName
Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
Related Other Helpful FAQs:
- Linux Rules for file names
- How you can run a c program in Linux?
- Linux/UNIX: Rules for naming file and directory names
- Howto apply a patch file to my Linux / UNIX source code
- How to compile program under Linux / UNIX / FreeBSD
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: BASH Shell, command shell, incoming text, lowercase, shell script, shell scripting, tr command, translate uppercase characters to lowercase, translate lowercase characters to uppercase, uppercase, variable data




Recent Comments
Today ~ 5 Comments
Today ~ 1 Comment
Yesterday ~ 1 Comment
Yesterday ~ 2 Comments
Yesterday ~ 10 Comments