Inhaltsverzeichnis

Bash: String konvertieren

Groß-/Kleinschreibung

tr

echo $a | tr '[A-Z]' '[a-z]'

awk

echo $a | awk '{print tolower($0)}'

Bash 4.0

echo ${a,,}

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

$ 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

$ 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