string 如何在 Bash 中将字符串转换为小写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2264428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a string to lower case in Bash?
提问by assassin
回答by ghostdog74
The are various ways:
有多种方式:
POSIX standard
POSIX标准
tr
tr
$ echo "$a" | tr '[:upper:]' '[:lower:]'
hi all
AWK
AWK
$ echo "$a" | awk '{print tolower($ echo "${a,,}"
hi all
)}'
hi all
Non-POSIX
非POSIX
You may run into portability issues with the following examples:
您可能会遇到以下示例的可移植性问题:
Bash 4.0
重击 4.0
$ echo "$a" | sed -e 's/\(.*\)/\L/'
hi all
# this also works:
$ sed -e 's/\(.*\)/\L/' <<< "$a"
hi all
sed
sed
$ echo "$a" | perl -ne 'print lc'
hi all
Perl
珀尔
lc(){
case "" in
[A-Z])
n=$(printf "%d" "'")
n=$((n+32))
printf \$(printf "%o" "$n")
;;
*)
printf "%s" ""
;;
esac
}
word="I Love Bash"
for((i=0;i<${#word};i++))
do
ch="${word:$i:1}"
lc "$ch"
done
Bash
重击
$ 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
Note: YMMV on this one. Doesn't work for me (GNU bash version 4.2.46 and 4.0.33 (and same behaviour 2.05b.0 but nocasematch is not implemented)) even with using shopt -u nocasematch;
. Unsetting that nocasematch causes [[ "fooBaR" == "FOObar" ]] to match OK BUT inside case weirdly [b-z] are incorrectly matched by [A-Z]. Bash is confused by the double-negative ("unsetting nocasematch")! :-)
注意:YMMV 关于这一点。即使使用shopt -u nocasematch;
. 取消设置 nocasematch 导致 [[ "fooBaR" == "FOObar" ]] 匹配 OK 但奇怪的是内部大小写 [bz] 被 [AZ] 错误匹配。Bash 被双重否定(“unsetting nocasematch”)弄糊涂了!:-)
回答by Paused until further notice.
In Bash 4:
在 Bash 4 中:
To lowercase
小写
$ 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
To uppercase
大写
$ 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
Toggle (undocumented, but optionally configurable at compile time)
切换(未记录,但可选择在编译时配置)
$ string="a few words"
$ declare -c string
$ string=$string
$ echo "$string"
A few words
Capitalize (undocumented, but optionally configurable at compile time)
大写(未记录,但可选择在编译时配置)
$ 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
Title case:
标题案例:
echo "Hi All" | tr "[:upper:]" "[:lower:]"
To turn off a declare
attribute, use +
. For example, declare +c string
. This affects subsequent assignments and not the current value.
要关闭declare
属性,请使用+
。例如,declare +c string
。这会影响后续分配而不是当前值。
The declare
options change the attribute of the variable, but not the contents. The reassignments in my examples update the contents to show the changes.
这些declare
选项会更改变量的属性,但不会更改内容。我的示例中的重新分配更新了内容以显示更改。
Edit:
编辑:
Added "toggle first character by word" (${var~}
) as suggested by ghostdog74.
添加${var~}
了ghostdog74建议的“按单词切换第一个字符”()。
Edit:Corrected tilde behavior to match Bash 4.3.
编辑:更正波浪号行为以匹配 Bash 4.3。
回答by shuvalov
a="$(tr [A-Z] [a-z] <<< "$a")"
回答by Ignacio Vazquez-Abrams
回答by nettux
I know this is an oldish post but I made this answer for another site so I thought I'd post it up here:
我知道这是一篇陈旧的帖子,但我为另一个网站做了这个答案,所以我想我会在这里发布:
UPPER -> lower: use python:
上 -> 下:使用 python:
b=`perl -e "print lc('$a');"`
Or Ruby:
或者红宝石:
b=`php -r "print strtolower('$a');"`
Or Perl (probably my favorite):
或 Perl(可能是我最喜欢的):
b=`echo "$a" | awk '{ print tolower() }'`
Or PHP:
或 PHP:
b=`echo "$a" | sed 's/./\L&/g'`
Or Awk:
或者 awk:
b=${a,,}
Or Sed:
或 Sed:
b=`echo "console.log('$a'.toLowerCase());" | node`
Or Bash 4:
或 Bash 4:
b=`echo "$a" | dd conv=lcase 2> /dev/null`
Or NodeJS if you have it (and are a bit nuts...):
或者 NodeJS,如果你有它(而且有点疯狂......):
b=`echo "print '$a'.upper()" | python`
You could also use dd
(but I wouldn't!):
你也可以使用dd
(但我不会!):
b=`echo "print '$a'.upcase" | ruby`
lower -> UPPER:
下 -> 上:
use python:
使用蟒蛇:
b=`perl -e "print uc('$a');"`
Or Ruby:
或者红宝石:
b=`php -r "print strtoupper('$a');"`
Or Perl (probably my favorite):
或 Perl(可能是我最喜欢的):
b=`echo "$a" | awk '{ print toupper() }'`
Or PHP:
或 PHP:
b=`echo "$a" | sed 's/./\U&/g'`
Or Awk:
或者 awk:
b=${a^^}
Or Sed:
或 Sed:
b=`echo "console.log('$a'.toUpperCase());" | node`
Or Bash 4:
或 Bash 4:
b=`echo "$a" | dd conv=ucase 2> /dev/null`
Or NodeJS if you have it (and are a bit nuts...):
或者 NodeJS,如果你有它(而且有点疯狂......):
b=$a:l
You could also use dd
(but I wouldn't!):
你也可以使用dd
(但我不会!):
b=$a:u
Also when you say 'shell' I'm assuming you mean bash
but if you can use zsh
it's as easy as
此外,当您说“shell”时,我假设您的意思是bash
但如果您可以使用zsh
它就像
echo $a:u
for lower case and
对于小写和
sed 's/.*/\L&/'
for upper case.
对于大写。
回答by Scott Smedley
In zsh:
在 zsh 中:
$ foo="Some STRIng";
$ foo=$(echo "$foo" | sed 's/.*/\L&/')
$ echo "$foo"
some string
Gotta love zsh!
一定要爱 zsh!
回答by devnull
Using GNU sed
:
使用 GNU sed
:
VARIABLE=$(echo "$VARIABLE" | tr '[:upper:]' '[:lower:]')
echo "$VARIABLE"
Example:
例子:
uppers=ABCDEFGHIJKLMNOPQRSTUVWXYZ
lowers=abcdefghijklmnopqrstuvwxyz
lc(){ #usage: lc "SOME STRING" -> "some string"
i=0
while ([ $i -lt ${#1} ]) do
CUR=${1:$i:1}
case $uppers in
*$CUR*)CUR=${uppers%$CUR*};OUTPUT="${OUTPUT}${lowers:${#CUR}:1}";;
*)OUTPUT="${OUTPUT}$CUR";;
esac
i=$((i+1))
done
echo "${OUTPUT}"
}
回答by hawkeye126
Pre Bash 4.0
预 Bash 4.0
Bash Lower the Case of a string and assign to variable
Bash 降低字符串的大小写并分配给变量
uc(){ #usage: uc "some string" -> "SOME STRING"
i=0
while ([ $i -lt ${#1} ]) do
CUR=${1:$i:1}
case $lowers in
*$CUR*)CUR=${lowers%$CUR*};OUTPUT="${OUTPUT}${uppers:${#CUR}:1}";;
*)OUTPUT="${OUTPUT}$CUR";;
esac
i=$((i+1))
done
echo "${OUTPUT}"
}
回答by technosaurus
For a standard shell (without bashisms) using only builtins:
对于仅使用内置函数的标准 shell(没有 bashisms):
A="HELLO WORLD"
typeset -l A=$A
And for upper case:
对于大写:
##代码##回答by c4f4t0r
In bash 4 you can use typeset
在 bash 4 中,您可以使用排版
Example:
例子:
##代码##