string 如何在 Bash 中将字符串从大写转换为小写?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11392189/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:32:17  来源:igfitidea点击:

How to convert a string from uppercase to lowercase in Bash?

stringbashshelluppercaselowercase

提问by raga

I have been searching to find a way to convert a string value from upper case to lower case. All the search results show approaches of using trcommand.

我一直在寻找一种将字符串值从大写转换为小写的方法。所有搜索结果都显示了使用tr命令的方法。

The problem with the trcommand is that I am able to get the result only when I use the command with echo statement. For example:

tr命令的问题是,只有当我使用带有 echo 语句的命令时,我才能获得结果。例如:

y="HELLO"
echo $y| tr '[:upper:]' '[:lower:]'

The above works and results in 'hello', but I need to assign the result to a variable as below:

以上工作并导致“你好”,但我需要将结果分配给一个变量,如下所示:

y="HELLO"
val=$y| tr '[:upper:]' '[:lower:]'
string=$val world

When assigning the value like above it gives me an empty result.

当分配像上面这样的值时,它给了我一个空的结果。

PS: My Bash version is 3.1.17

PS:我的 Bash 版本是 3.1.17

回答by kev

If you are using bash 4 you can use the following approach:

如果您使用的是 bash 4,则可以使用以下方法:

x="HELLO"
echo $x  # HELLO

y=${x,,}
echo $y  # hello

z=${y^^}
echo $z  # HELLO

Use only one ,or ^to make the first letter lowercaseor uppercase.

仅使用一个,or^来制作第一个字母lowercaseor uppercase

回答by Rody Oldenhuis

The correct way to implement your code is

实现代码的正确方法是

y="HELLO"
val=$(echo "$y" | tr '[:upper:]' '[:lower:]')
string="$val world"

This uses $(...)notation to capture the output of the command in a variable. Note also the quotation marks around the stringvariable -- you need them there to indicate that $valand worldare a single thing to be assigned to string.

这使用$(...)符号来捕获变量中命令的输出。还要注意string变量周围的引号——您需要在那里使用它们来表示$valworld是要分配给的单个事物string

If you have bash4.0 or higher, a more efficient & elegant way to do it is to use bashbuiltin string manipulation:

如果您有bash4.0 或更高版本,更有效和优雅的方法是使用bash内置字符串操作:

y="HELLO"
string="${y,,} world"

回答by DevSolar

Note that trcan only handle plain ASCII, making any tr-based solution fail when facing international characters.

请注意,tr只能处理纯 ASCII,因此tr在面对国际字符时,任何基于解决方案的解决方案都会失败。

Same goes for the bash 4 based ${x,,}solution.

基于 bash 4 的${x,,}解决方案也是如此。

The awktool, on the other hand, properly supports even UTF-8 / multibyte input.

awk另一方面,该工具甚至正确支持 UTF-8 / 多字节输入。

y="HELLO"
val=$(echo "$y" | awk '{print tolower(
 x=`echo "$y" | tr '[:upper:]' '[:lower:]'` 
)}') string="$val world"

Answer courtesy of liborw.

答案由liborw 提供

回答by Brian Agnew

Why not execute in backticks ?

为什么不以反引号执行?

CAPITALIZED=`echo "${y}" | tr '[a-z]' '[A-Z]'`

This assigns the result of the command in backticks to the variable x. (i.e. it's not particular to trbut is a common pattern/solution for shell scripting)

这会将命令的结果以反引号形式分配给变量x。(即它不是特定的,tr但它是 shell 脚本的常见模式/解决方案)

You can use $(..)instead of the backticks. See herefor more info.

您可以使用$(..)而不是反引号。请参阅此处了解更多信息。

回答by BoomShadow

I'm on Ubuntu 14.04, with Bash version 4.3.11. However, I still don't have the fun built in string manipulation ${y,,}

我使用的是 Ubuntu 14.04,Bash 版本为 4.3.11。但是,我仍然没有内置字符串操作的乐趣${y,,}

This is what I used in my script to force capitalization:

这是我在脚本中用来强制大写的内容:

$ declare -u FOO=AbCxxx
$ echo $FOO
ABCXXX

回答by Steven Powell

If you define your variable using declare (old: typeset) then you can state the case of the value throughout the variable's use.

如果您使用声明(旧:排版)定义变量,那么您可以在整个变量的使用过程中说明值的大小写。

y="HELLO"
val=$(echo $y | tr '[:upper:]' '[:lower:]')
string="$val world"

"-l"does lc.

"-l"是吗?

回答by Ray Moncada

This worked for me. Thank you Rody!

这对我有用。谢谢罗迪!

string="${val}_world"

one small modification, if you are using underscore next to the variable You need to encapsulate the variable name in {}.

一个小的修改,如果你在变量旁边使用下划线,你需要将变量名封装在{}中。

##代码##