LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 07-18-2007, 01:16 PM   #1
powah
Member
 
Registered: Mar 2005
Distribution: FC, Gentoo
Posts: 276

Rep: Reputation: 30
bash : change first character to upper case


How to use a variable in a command in bash?
e.g.
$role="operator"
/usr/bin/passwd -u $role >& /dev/null

How to change only the first character to upper case?
e.g.
$Role=$role
echo "$Role account enabled successfully."
 
Old 07-18-2007, 02:21 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Try using sed. For more info:

Code:
man sed
Hope this helps.
 
Old 07-18-2007, 03:06 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Quote:
How to use a variable in a command in bash?
e.g.
$role="operator"
/usr/bin/passwd -u $role >& /dev/null
Your example is almost correct.
This: $role="operator" should be:
role="operator" (the variable has no leading $).

Quote:
How to change only the first character to upper case?
Using a perl one-liner is probably the easiest:

$ echo $role
operator

$ echo $role | perl -ne 'print ucfirst($_)'
Operator


If perl is not an option, you could write a function:
Code:
capitalize_first ()          
{                            
  string0="$@"               
  firstchar=${string0:0:1}   
  string1=${string0:1}       
  FirstChar=`echo "$firstchar" | tr a-z A-Z`
  echo "$FirstChar$string1"  
}
The above code separates the first char from the input (firstchar=${string0:0:1} and string1=${string0:1}), capitalizes the first char (FirstChar=`echo "$firstchar" | tr a-z A-Z`) and returns the result (echo "$FirstChar$string1").

Both variables and 'normal' input is accepted:

$ echo $role
operator

$ capitalize_first $role
Operator

$ capitalize_first operator
Operator


BTW: Here's a bash very usefull bash guide: Advanced Bash-Scripting Guide

Hope this helps.

Last edited by druuna; 07-18-2007 at 03:07 PM.
 
Old 07-18-2007, 04:22 PM   #4
powah
Member
 
Registered: Mar 2005
Distribution: FC, Gentoo
Posts: 276

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by druuna
Hi,



Your example is almost correct.
This: $role="operator" should be:
role="operator" (the variable has no leading $).


Using a perl one-liner is probably the easiest:

$ echo $role
operator

$ echo $role | perl -ne 'print ucfirst($_)'
Operator


...

Hope this helps.
Thank you for your very useful comments.

Last edited by powah; 07-18-2007 at 04:24 PM.
 
Old 07-18-2007, 04:28 PM   #5
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Quote:
Originally Posted by powah
How to use a variable in a command in bash?
e.g.
$role="operator"
/usr/bin/passwd -u $role >& /dev/null

How to change only the first character to upper case?
e.g.
$Role=$role
echo "$Role account enabled successfully."
Or use zsh instead:

Code:
$ role="operator"
$ echo "${(C)role}"
Operator
 
Old 07-18-2007, 08:18 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you have Python:
Code:
var=$(echo "abc" | python -c "print raw_input().capitalize()")
 
  


Reply


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
ls only files starting with upper case character sancho1980 Linux - Newbie 19 12-29-2019 10:24 AM
Bash: change case graemef Programming 9 04-06-2010 08:48 PM
Why are all my upper case files being shown as lower case?? [Kernel 2.6.9-1.667 FC3] t3gah Fedora 4 03-11-2005 04:09 PM
Lower case to upper case letter sudhasmyle Programming 1 12-03-2004 04:15 AM
Java: Change first character in word to upper AMMullan Programming 4 04-05-2004 03:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration