bash 将字符串转换为可变小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34826253/
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
Convert string in variable lower case
提问by meteorBuzz
Using bash version 3.2.57(1)-release (x86_64-apple-darwin14)
使用 bash 版本 3.2.57(1)-release (x86_64-apple-darwin14)
How can I 're-assign' or 'change' the existing value read into a variable.
如何“重新分配”或“更改”读入变量的现有值。
If the user inputs the string IAmString
, I'd like propInput
to store the value iamstring
. I'm simply printing to the console just for example sake.
如果用户输入字符串IAmString
,我想propInput
存储值iamstring
。我只是为了举例而打印到控制台。
read userInput
echo ${userInput} | tr '[:upper:]' '[:lower:]'
回答by Aaron
You should store the output of your commands :
您应该存储命令的输出:
read userInput
userInput=$(echo "$userInput" | tr '[:upper:]' '[:lower:]')
回答by yftse
Ii'm surprised that doesn't already work...
我很惊讶这还行不通...
Did you have trouble storing the value?
您在存储值时遇到问题吗?
If so, you'd have to:
如果是这样,你必须:
userInput=$(echo ${userInput} | tr '[:upper:]' '[:lower:]')
But it looks like a mac environment. What results are you getting?
但它看起来像一个mac环境。你得到什么结果?
Edit, added userInput=$()
and restructured to better inquire.
编辑、添加userInput=$()
和重组以更好地查询。