Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
12-19-2004, 06:19 PM
|
#1
|
Member
Registered: Oct 2003
Location: USA Fresno Callifornia
Distribution: Gentoo (workstation), ArchLinux (file/mail server), Freebsd (web server), Ubuntu (laptop)
Posts: 115
Rep:
|
bash scripting capitalizing first letter or each word in a string
I am looking for a way to capitalize the first letter of each word that is in a string in bash. I know how to make a letter or string upper or lower case but I do not know how to break apart a string and do this just on the first character. Any help is much appreciated.
|
|
|
12-19-2004, 06:37 PM
|
#2
|
Member
Registered: Dec 2002
Location: In front of a computer
Distribution: UPS, DHL, FedEx
Posts: 466
Rep:
|
A="asdf asdf asdf";
for i in $A; do B=`echo "${i:0:1}" | tr a-z A-Z`${i:1}; echo -n "$B "; done
what is considered a "space" in bash is controled by the IFS variable.
Last edited by niknah; 12-20-2004 at 12:04 AM.
|
|
|
12-19-2004, 11:09 PM
|
#3
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
I had better luck with this setup.
A="abcdef abcdef abcdef";
for i in $A; do B=`echo -n "${i:0:1}" | tr "[:lower:]" "[:upper:]"`; echo -n "${B}${i:1} "; done
|
|
|
12-19-2004, 11:45 PM
|
#4
|
Member
Registered: Oct 2003
Location: USA Fresno Callifornia
Distribution: Gentoo (workstation), ArchLinux (file/mail server), Freebsd (web server), Ubuntu (laptop)
Posts: 115
Original Poster
Rep:
|
Amazing.  Thanks for the help!
|
|
|
01-23-2011, 03:01 AM
|
#5
|
LQ Newbie
Registered: Jan 2011
Posts: 1
Rep:
|
Of course it's very simple to capitalize entire strings:
Code:
echo "string" | tr [:lower:] [:upper:]
in csh (freebsd), you have do silly things like escape characters:
Code:
echo "string" | tr \[\:lower\:\] \[\:upper\:\]
and instead of just , you would
Code:
@bsd> set A="string"
|
|
|
01-23-2011, 04:44 AM
|
#6
|
LQ Newbie
Registered: Apr 2010
Posts: 18
Rep:
|
bash4 should be able to do so using ${parameter^^pattern}
using an array
Code:
A="abcdef abcdef abcdef"
B=( $A )
echo "${B[@]^}"
Abcdef Abcdef Abcdef
Last edited by everToulouse; 01-23-2011 at 05:26 AM.
|
|
|
06-16-2011, 09:58 AM
|
#7
|
LQ Newbie
Registered: Jun 2011
Posts: 1
Rep: 
|
Thank you everToulouse ! Exactly what I was looking for ^^
|
|
|
All times are GMT -5. The time now is 07:37 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|