linux - bash 脚本的格式化输出

标签 linux bash scripting

我尝试使用 bash 脚本显示某些详细信息,但 bash 脚本的输出与终端的输出不同。

终端输出:

ubuntu@ubuntu:~/ubin$ cat schedule.text  | grep 09/06/12
Sat 09/06/12 Russia           00:15 Czech Republic   A
Sat 09/06/12 Netherlands      21:30 Denmark          B
ubuntu@ubuntu:~/ubin$ 

Bash 脚本输出:

ubuntu@ubuntu:~/ubin$ bash fixture.sh 
Sat 09/06/12 Russia 00:15 Czech Republic A Sat 09/06/12 Netherlands 21:30 Denmark B
ubuntu@ubuntu:~/ubin$ 

正如您所看到的,bash 脚本的输出与终端的输出不同。我的 bash 脚本输出将所有内容都放在一行中。

fixture.sh:

A=$(date +%d/%m/%y) #get today's date in dd/mm/yy fmt
fixture=$(cat /home/ubuntu/ubin/schedule.text | grep $A)
echo $fixture

所以,我的问题是如何使 bash 脚本输出类似于终端输出?

最佳答案

使用双引号:

echo "$fixture"

当变量fixture嵌入换行符并且不加引号时,bash将其分割成不同的参数来回显。为了简化,假设固定装置是字符串“a\nb”。如果没有引号,bash 会向 echo 传递两个参数:ab。使用引号时,bash 只传递一个参数并且不会丢弃换行符。

关于linux - bash 脚本的格式化输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10956096/

相关文章:

linux - 在 Ubuntu 12.0.4 上获取库源 tarball 而不是执行 sudo apt get

linux - 如何在 GMT 中制作同心圆

linux - Ubuntu 自动安装 sh 脚本,如何将 apt-get 或其他命令的答案传递给配置选项?

linux - 使用特定扩展名创建脚本文件

linux - 在 Linux 中使用 awk 从命令输出中查找特定数据

c++ - nfq_get_payload 是如何构造它的返回数据的?

linux - 如何根据搜索在文件内容中附加字符串

scripting - 通过 F# 解释脚本

scripting - 是否有 curl/wget 选项可以防止在出现 http 错误时保存文件?

linux - 无法在 git hook 预推脚本调用的 shell 脚本中获取用户的输入