如何在 bash shell 中格式化字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24438196/
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 format a string in bash shell?
提问by Ullan
I am trying to format a variable in linux
我正在尝试在 linux 中格式化一个变量
str="Initial Value = 168"
echo "New Value=$(echo $str| cut -d '=' -f2);">>test.txt
I am expecting the following output
我期待以下输出
Value = 168;
But instead get
而是得到
Value = 168 ^M;
回答by Elliott Frisch
Don't edit your bash script on DOS or Windows. You can run dos2unix
on the bash script. The issue is that Windows uses "\r\n" as a line separator, Linux uses "\n". You can also manually remove the "\r" characters in an editor on Linux.
不要在 DOS 或 Windows 上编辑您的 bash 脚本。您可以dos2unix
在 bash 脚本上运行。问题是 Windows 使用“\r\n”作为行分隔符,Linux 使用“\n”。您还可以在 Linux 上的编辑器中手动删除“\r”字符。
回答by Tiago Lopo
Try this:
尝试这个:
#! /bin/bash
str="Initial Value = 168"
awk '{print "="}' <<< $str > test.txt
Output:
输出:
cat test.txt
Value=168
I've got comment saying that it doesn't address ^M, I actually does:
我有评论说它没有解决 ^M,我实际上是:
echo -e 'Initial Value = 168 \r' | cat -A
Initial Value = 168 ^M$
After awk
:
之后awk
:
echo -e 'Initial Value = 168 \r' | awk '{print "="}' | cat -A
Value=168$
回答by ghoti
First off, always quote your variables.
首先,总是引用你的变量。
#!/bin/bash
str="Initial Value = 168"
echo "New Value=$(echo "$str" | cut -d '=' -f2);"
For me, this results in the output:
对我来说,这会导致输出:
New Value= 168;
If you're getting a carriage return between the digits and the semicolon, then something may be wrong with your echo
, or perhaps your input data is not what you think it is. Perhaps you're editing your script on a Windows machine and copying it back, and your variable assignment is getting DOS-style newlines. From the information you've provided in your question, I can't tell.
如果您在数字和分号之间得到回车,那么您的 可能有问题echo
,或者您的输入数据可能不是您认为的那样。也许您正在 Windows 机器上编辑脚本并将其复制回来,而您的变量分配正在获得 DOS 样式的换行符。从你在问题中提供的信息来看,我无法判断。
At any rate I wouldn't do things this way. I'd use printf
.
无论如何,我不会这样做。我会用printf
.
#!/bin/bash
str="Initial Value = 168"
value=${str##*=}
printf "New Value=%d;\n" "$value"
The output of printf
is predictable, and it handily strips off gunk like whitespace when you don't want it.
的输出printf
是可预测的,当您不想要它时,它可以轻松地去除像空白一样的垃圾。
Note the replacement of your cut
. The functionality of bash built-ins is documented in the Bash man page under "Parameter Expansion", if you want to look it up. The replacement I've included here is not preciselythe same functionality as what you've got in your question, but is functionally equivalent for the sample data you've provided.
请注意替换您的cut
. bash 内置函数的功能记录在“参数扩展”下的 Bash 手册页中,如果您想查找的话。我在此处包含的替换功能与您在问题中获得的功能不完全相同,但在功能上与您提供的示例数据相同。
回答by David C. Rankin
str="Initial Value = 168"
newstr="${str##* }"
echo "$newstr" # 168
pattern matching is the way to go.
模式匹配是要走的路。