$ 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
到大写
$ 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
切换(未记录,但可在编译时选择配置)
$ 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
大写(未记录,但可在编译时选择配置)
$ string="a few words"$ declare -c string$ string=$string$ echo "$string"A few words
标题案例:
$ 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
$ string="a FeW WOrdS"$ string=${string,,}$ string=${string~}$ echo "$string"A few words
# Like echo, but converts to lowercaseecholcase () {tr [:upper:] [:lower:] <<< "${*}"}
# Takes one arg by reference (var name) and makes it lowercaselcase () {eval "${1}"=\'$(echo ${!1//\'/"'\''"} | tr [:upper:] [:lower:] )\'}
#!/bin/bash
# lowerupper.sh
# Prints the lowercase version of a charlowercaseChar(){case "$1" in[A-Z])n=$(printf "%d" "'$1")n=$((n+32))printf \\$(printf "%o" "$n");;*)printf "%s" "$1";;esac}
# Prints the lowercase version of a sequence of stringslowercase() {word="$@"for((i=0;i<${#word};i++)); doch="${word:$i:1}"lowercaseChar "$ch"done}
# Prints the uppercase version of a charuppercaseChar(){case "$1" in[a-z])n=$(printf "%d" "'$1")n=$((n-32))printf \\$(printf "%o" "$n");;*)printf "%s" "$1";;esac}
# Prints the uppercase version of a sequence of stringsuppercase() {word="$@"for((i=0;i<${#word};i++)); doch="${word:$i:1}"uppercaseChar "$ch"done}
# The functions will not add a new line, so use echo or# append it if you want a new line after printing
# Printing stuff directlylowercase "I AM the Walrus!"$'\n'uppercase "I AM the Walrus!"$'\n'
echo "----------"
# Printing a varstr="A StRing WITH mixed sTUFF!"lowercase "$str"$'\n'uppercase "$str"$'\n'
echo "----------"
# Not quoting the var should also work,# since we use "$@" inside the functionslowercase $str$'\n'uppercase $str$'\n'
echo "----------"
# Assigning to a varmyLowerVar="$(lowercase $str)"myUpperVar="$(uppercase $str)"echo "myLowerVar: $myLowerVar"echo "myUpperVar: $myUpperVar"
echo "----------"
# You can even do stuff likeif [[ 'option 2' = "$(lowercase 'OPTION 2')" ]]; thenecho "Fine! All the same!"elseecho "Ops! Not the same!"fi
exit 0
运行此操作后的结果:
$ ./lowerupper.shi am the walrus!I AM THE WALRUS!----------a string with mixed stuff!A STRING WITH MIXED STUFF!----------a string with mixed stuff!A STRING WITH MIXED STUFF!----------myLowerVar: a string with mixed stuff!myUpperVar: A STRING WITH MIXED STUFF!----------Fine! All the same!