I've a small shell script and I'd like to convert all incoming user input to lowercase using a shell script. How do I convert uppercase words or strings to a lowercase or vise versa under Unix / Linux BASH shell?
Use the 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 In a File From UPPER to lowercase:
Type the following command at shell prompt:
$ tr '[:upper:]' '[:lower:]' < input.txt > output.txt
$ cat output.txt
Task: Convert Data Stored in a Shell Variable From UPPPER to lowercase:
Type the following command:
$ echo $VAR_NAME | tr '[:upper:]' '[:lower:]'
$ echo $VAR_NAME | tr '[:lower:]' '[:upper:]'
Bash 4 Uppercase To Lowercase or Viceversa
Type the following commands to convert $y into uppercase:
y="this Is A test" echo "${y^^}"
Sample outputs:
THIS IS A TEST
Type the following commands to convert $y into lowercase:
y="THIS IS a TeSt" echo "${y,,}"
Sample outputs:
this is a test
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 # Bash version 4 user should use builtin
Recommended readings:
- See tr command shell scripting example for more information.
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop










![Bash Shell Scripting Disable Control-C [ CTRL+C ] Keys](http://s13.cyberciti.org/images/shared/rp/3/20.jpg)



{ 18 comments… read them below or add one }
This work fine when using latin characters, but not when using Unicode characters. For example a string with Cyrillic characters (“Английский”) doesn’t get converted. Any tips on how to do that?
Well, tr has no way of knowing what are uppercase and what are lowercase cyrillic characters. You need to provide it with this information.
tr
The example above is equivalent to:
Damn I didn’t notice this thing had html tags enabled and used angled brackets in my post. I meant:
tr [all uppercase cyrillic characters] [their lowercase equivalent]
instead of just ‘tr’
Is there any options so that Other than first letter of a word get converted to lowercase
show the correct program fully
Is there any options so that Other than first letter of a word get converted to lowercase
You mean preserve the first and translate the rest? How about:
echo $(echo $VAR_NAME | cut -c1)$(echo $VAR_NAME | cut -c2- | tr [A-Z] [a-z])If you just want to change the case of the file extension, here is one way to do it:
for f in *.TXT; do mv $f `basename $f .TXT`.txt; done;
Thanks for this opportunity, to ask this question, This is my question.
HOW DO I TRANSLATE TEXT IN A FILE FROM ONE CASE TO ANOTHER.
cat | tr [[:upper:]] [[:lower:]]
hey guys your info has been really helpful. I m an Mca student and am vry good in C lang. Since it is embedd in ma brain, i feel it very hard to learn another script lang llike Unix but i want to perforn in Unix too. Could please help me by providn som basic tips so i can learn shell progming quickly.
Ur help vl b greatly appreciated
Nice example it helped my issue
In bash 4, the easiest and by far the most elegant way to do this for lowercase is:
echo ${string,,*}
and for uppercase:
echo ${string^^*}
This doesn’t work with the old Solaris version of tr, you need to use the GNU version in /usr/xpg4/bin on Solaris.
Is this a backslash, or a single quote?
$ echo ibm | awk ‘{ print toupper($1); }’
IBM
Works also.
perl -pe ‘$_=uc’
I need to write a shell script that copies a list of files from one directory to another. During copying the script should capitalize the first letter of the filename and lowercase all other letters.
I need the script as soon as possible.Please