Saturday, August 2, 2014

bash:: tolowercase,to uppercase

http://stackoverflow.com/questions/2264428/converting-string-to-lower-case-in-bash-shell-scripting

To lowercase
$ string="A FEW WORDS"
$ echo ${string,}
a FEW WORDS
$ echo ${string,,}
a few words
$ echo ${string,,[AEIUO]}
a FeW WoRDS

$ string="A Few Words"
$ declare -l string
$ string=$string; echo $string
a few words
To uppercase
$ string="a few words"
$ echo ${string^}
A few words
$ echo ${string^^}
A FEW WORDS
$ echo ${string^^[aeiou]}
A fEw wOrds

$ string="A Few Words"
$ declare -u string
$ string=$string; echo $string
A FEW WORDS
Toggle (undocumented)
$ string="A Few Words"
$ echo ${string~~}
a fEW wORDS
$ string="A FEW WORDS"
$ echo ${string~}
a fEW wORDS
$ string="a few words"
$ echo ${string~}
A Few Words
Capitalize (undocumented)
$ string="a few words"
$ declare -c string
$ string=$string
$ echo $string
A few words
Title case:
$ string="a few words"
$ string=($string)
$ string=${string[@]^}
$ echo $string
A Few Words

$ declare -c string
$ string=(a few words)
$ echo ${string[@]}
A Few Words

No comments:

Post a Comment