簡體   English   中英

如何在bash shell中編寫帶有一些變量的文件?

[英]How to write a file with some variables in bash shell?

我試圖通過使用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

您的代碼是正確且有效的。

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

對於多行輸出而不是使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM