Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 12-19-2004, 06:19 PM   #1
Registered: Oct 2003
Location: USA Fresno Callifornia
Distribution: Gentoo (workstation), ArchLinux (file/mail server), Freebsd (web server), Ubuntu (laptop)
Posts: 115

Rep: Reputation: 16
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.
Old 12-19-2004, 06:37 PM   #2
Registered: Dec 2002
Location: In front of a computer
Distribution: UPS, DHL, FedEx
Posts: 466

Rep: Reputation: 38
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.
Old 12-19-2004, 11:09 PM   #3
Senior Member
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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
Old 12-19-2004, 11:45 PM   #4
Registered: Oct 2003
Location: USA Fresno Callifornia
Distribution: Gentoo (workstation), ArchLinux (file/mail server), Freebsd (web server), Ubuntu (laptop)
Posts: 115

Original Poster
Rep: Reputation: 16
Amazing. Thanks for the help!
Old 01-23-2011, 03:01 AM   #5
LQ Newbie
Registered: Jan 2011
Posts: 1

Rep: Reputation: 0
Of course it's very simple to capitalize entire strings:
echo "string" | tr [:lower:] [:upper:]
in csh (freebsd), you have do silly things like escape characters:
echo "string" | tr \[\:lower\:\] \[\:upper\:\]
and instead of just
@bsd> A="string"
, you would
@bsd> set A="string"
Old 01-23-2011, 04:44 AM   #6
LQ Newbie
Registered: Apr 2010
Posts: 18

Rep: Reputation: 5
bash4 should be able to do so using ${parameter^^pattern}

using an array
A="abcdef abcdef abcdef"
B=( $A )
echo "${B[@]^}"
Abcdef Abcdef Abcdef

Last edited by everToulouse; 01-23-2011 at 05:26 AM.
Old 06-16-2011, 09:58 AM   #7
LQ Newbie
Registered: Jun 2011
Posts: 1

Rep: Reputation: Disabled
Thank you everToulouse ! Exactly what I was looking for ^^


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a word in a string with bash orgazmo Programming 6 06-07-2005 10:19 AM
oh god, the three letter word, ATI onelung02 SUSE / openSUSE 8 10-12-2004 05:17 PM
PHP delete letter from string Boffy Programming 2 04-30-2004 12:12 PM
/proc/version/four/letter/word opioid Linux - Security 2 02-27-2004 08:28 PM
sed help, replacing a letter with a word GridX Linux - Newbie 1 09-24-2003 10:21 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:57 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration