lc(){
case "$1" in
[A-Z])
n=$(printf "%d" "'$1")
n=$((n+32))
printf \\$(printf "%o" "$n")
;;
*)
printf "%s" "$1"
;;
esac
}
word="I Love Bash"
for((i=0;i<${#word};i++))
do
ch="${word:$i:1}"
lc "$ch"
done
3. ucwords
echo 'justcode ikeepstudying com is my own site !' | tr " " "\n" | nawk ' { out = out" "toupper(substr($0,1,1))substr($0,2) } END{ print substr(out,2) } '
#Justcode Ikeepstudying Com Is My Own Site !
sed 's/\(.\)/\U\1/' <<< "justcode.ikeepstudying.com is my own site!"
#Justcode.ikeepstudying.com is my own site!
其他:
#Toggle (undocumented, but optionally configurable at compile time)
$ 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, but optionally configurable at compile time)
$ 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
$ string="a FeW WOrdS"
$ string=${string,,}
$ string=${string~}
$ echo "$string"
本系列适合Linux初学者,属于Linux入门级教程,主要介绍了Shell的分类、语法格式以及脚本的使用和编写格式等。 不断更新中,是Shell学习的必读经典教程。 Linux Shell系列教程之(一)Shell简介 Linux Shell系列教程之(二)第一个Shell脚本 Linux Shell系列教程之(三)Shell变量 Linux…
不错!