LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 09-28-2009, 04:29 PM   #1
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Rep: Reputation: 18
How to output first character of a word in capital?


Kindly consider:
Code:
-bash-2.05b# echo $v | tr [a-z] [A-Z]
HELLO
-bash-2.05b#
Well, I want the output in the format given below:

Code:
Hello
How can we achieve it?

In fact, I am doing this:

Code:
echo "Welcome, $USER!"
Output from the script file:

Code:
-bash-2.05b# ./welcome.sh
Welcome, root!
-bash-2.05b#
and want to display the user's name as Root instead of root or whatevever value is stored in the variable being used.

Any ideas?
 
Old 09-28-2009, 06:22 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
If you want to use commands like tr, then you can do this:
Code:
echo $USER | head -c 1 | tr [a-z] [A-Z]; echo $USER | tail -c +2
Or you might use perl:
Code:
perl -e "print ucfirst($USER)"

Last edited by neonsignal; 09-29-2009 at 03:09 AM. Reason: fixed up variable reference
 
Old 09-28-2009, 06:23 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,151

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Try this:
Code:
#!/bin/bash
name=$USER
[ $# -ne 0 ] && name=$1
echo Welcome ${name^}!
(Actually, just echo Welcome ${USER^}! should do. The fluff in the code is so it can be tested.)
 
Old 09-28-2009, 09:01 PM   #4
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Original Poster
Rep: Reputation: 18
This doesn't yeild the desired result:

Code:
-bash-2.05b# echo $USER | head -c 1 | tr [a-z] [A-Z]; echo $v | tail -c +1
r
-bash-2.05b# echo $USER | head -c 1 | tr [a-z] [A-Z]
-bash-2.05b#  echo $v | tail -c +1

-bash-2.05b# v=hi
-bash-2.05b# echo $v | tail -c +1
hi
-bash-2.05b#

Hey, this does one works!
Code:
-bash-2.05b# perl -e "print ucfirst($USER)"
Root-bash-2.05b#

Thanks buddy!

Quote:
Originally Posted by neonsignal View Post
If you want to use commands like tr, then you can do this:
Code:
echo $USER | head -c 1 | tr [a-z] [A-Z]; echo $v | tail -c +1
Or you might use perl:
Code:
perl -e "print ucfirst($USER)"
 
Old 09-28-2009, 09:06 PM   #5
Hi_This_is_Dev
Member
 
Registered: May 2009
Location: India
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254

Original Poster
Rep: Reputation: 18
After running the script I get this error:

Code:
-bash-2.05b# cat cap.sh
me=$USER
[ $# -ne 0 ] && name=$1
echo Welcome ${name^}!

-bash-2.05b#
-bash-2.05b# ./cap.sh
./cap.sh: line 3: ${name^}!: bad substitution
-bash-2.05b#

Quote:
Originally Posted by PTrenholme View Post
Try this:
Code:
#!/bin/bash
name=$USER
[ $# -ne 0 ] && name=$1
echo Welcome ${name^}!
(Actually, just echo Welcome ${USER^}! should do. The fluff in the code is so it can be tested.)



By the way, thanks to you!
 
Old 09-28-2009, 09:44 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948Reputation: 1948
"${VARIABLE^}" is a new bash version 4 substitution. It doesn't exist in previous releases.

The OP's prompt shows that he's using bash 2.05, so it naturally won't work for him. But it does lead to the question as to why he's running such an old version.
 
  


Reply

Tags
proper, word


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
variable length string using GD (word wrap, carriage return, word/character count)? frieza Programming 1 02-14-2009 05:21 PM
how to replace special character by any word in a string. mksc Linux - Newbie 1 08-21-2008 02:33 AM
Inspected a character on a particular position in a word kushalkoolwal Programming 10 07-03-2008 08:37 PM
find a string followed by any word character in bash bryan.out.there Programming 2 07-12-2006 06:36 AM
Java: Change first character in word to upper AMMullan Programming 4 04-05-2004 03:16 PM


All times are GMT -5. The time now is 11:37 AM.

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