bash - 字符串插值在 bash 中如何工作?

标签 bash

我正在尝试在字符串中插入参数值。为了通用性和简单性,让我举一个简单的例子。

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/

相关文章:

python - MythTV 的 Bash 脚本需要 Python 依赖项

linux - 如何在执行 bash 脚本时自动 git fetch 和重置

linux - 如何在 var 的末尾 append 双引号 (")

linux - Bash 脚本,仅在以前未下载文件时才从 FTP 下载文件

bash - 无法使用协议(protocol)规范写入 Redis

bash - 如何保持ffmpeg裁剪检测的最高值

linux - 将多行回显到命令中?

linux - 使 *.mp3 的开始和结束段没有声音的最佳方法?

linux - 遍历 ls 输出不会在 bash 中发生

Bash shell 脚本嵌套 while 循环与 IFS