我正在尝试在字符串中插入参数值。为了通用性和简单性,让我举一个简单的例子。
size="40"
#Yes, variable size is a string
echo "Total Bytes $size"
#^ works fine: outputs Total Bytes: 40
echo "Total Bytes: $size bytes"
#outputs bytesBytes: 40
#The string " bytes" is inserted at the beginning overwriting the other string- why?
我已经尝试了这些命令的多种变体,但似乎没有一个起作用,这让我相信我正在犯一些典型的菜鸟错误。 欢迎提出任何建议。
最佳答案
您已使用 CRLF
行结尾保存文件。
尝试 dos2unix your_script_file
如果你的发行版有它。
否则这是您的解决方案:sed -i $'s/\r//' your_script_file
我猜你的脚本是从 curl
获得的,它是一个 dangerous practice .不过,我现在不会详细介绍这些细节。
您可以简单地将 curl
输出通过 tr -d '\r'
进行管道传输,以获得您想要的正确脚本。
关于bash - 字符串插值在 bash 中如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22481663/