我尝试使用 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 传递两个参数:a
和 b
。使用引号时,bash 只传递一个参数并且不会丢弃换行符。
关于linux - bash 脚本的格式化输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10956096/