如何在bash shell中写一些变量的文件?

时间:2019-04-19 06:05:47

标签: bash

我正在尝试通过使用sh文件将变量打印到我的文件中,但是它不起作用。 这是非常简单的代码。我该如何解决?我一点也不明白。该脚本按我的预期在命令中起作用。但是当我将脚本移至sh文件时,它显示出完全不同的结果。

#! /bin/bash
var=12345
echo -e "first sentence
second sentence
$var:$var th sentence
"

我期望

first sentence
second sentence
12345:12345 th sentence

但这显示了我

first sentence
second sentence
 th sentence

2 个答案:

答案 0 :(得分:0)

您的代码正确有效。

~# bash s.sh
first sentence
second sentence
12345:12345 th sentence

答案 1 :(得分:0)

要进行多行输出而不是使用echo的唯一其他建议是使用 heredoc 。只要您不对单引号'EOF'进行单引号扩展,例如

#!/bin/bash

var=12345

cat << EOF
first sentence
second sentence
$var:$var th sentence
EOF

使用/输出示例

$ bash heredoc.sh
first sentence
second sentence
12345:12345 th sentence
相关问题