cat> somefile <<“ EOF”或EOF,在bash shell上产生不同的结果

我的路

我编写以下shell脚本:

#!/bin/sh

TEST_VAR="HELLO"

cat > test-1.txt << EOF
TEST_VAR is ${TEST_VAR}
EOF

cat > test-2.txt << "EOF"
TEST_VAR is ${TEST_VAR}
EOF

但是我发现结果是不同的:

test-1.txtTEST_VAR is HELLO

和:

test-2.txtTEST_VAR is ${TEST_VAR}

有人可以帮忙解释一下吗?

杰伊·贾戈特

在下面的行导致了问题:

cat > test-2.txt << "EOF"

原因是在man bash中:引用时没有参数扩展。

Here Documents
   This type of redirection instructs the shell to read input from the current source until a line containing only delimiter (with no trailing blanks) is seen.  All of the  lines  read
   up to that point are then used as the standard input for a command.

   The format of here-documents is:

          <<[-]word
                  here-document
          delimiter

   No  parameter and variable expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word.  If any characters in word are quoted, the delimiter is
   the result of quote removal on word, and the lines in the here-document are not expanded.  If word is unquoted, all lines of the here-document are subjected to parameter  expansion,
   command substitution, and arithmetic expansion, the character sequence \<newline> is ignored, and \ must be used to quote the characters \, $, and `.

   If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing delimiter.  This allows here-documents within shell scripts
   to be indented in a natural fashion.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

@ <(cat <<-EOF在Bash中做什么?

来自分类Dev

为什么不使用<< EOF代替cat << EOF?

来自分类Dev

为什么不使用<< EOF代替cat << EOF?

来自分类Dev

如何在cat << EOF> Test.html中使用bash $ variable

来自分类Dev

如何在 cat <<EOF > Test.html 中使用 bash $variable

来自分类Dev

子shell中的bash -c给出了EOF解析错误

来自分类Dev

为什么ERR陷阱会从“ echo <(cat << EOF”

来自分类Dev

如果通过cat管道传输,则grep直到EOF才输出

来自分类Dev

Bash:带有`read`命令和def的Shell脚本,包裹在<<-EOF中

来自分类Dev

上次创建的远程文件上的Bash cat

来自分类Dev

Shell脚本ssh $ SERVER >> EOF

来自分类Dev

Java与EOF运行shell命令

来自分类Dev

Unix shell脚本检查EOF

来自分类Dev

Shell脚本ssh $ SERVER >> EOF

来自分类Dev

EOF上的std :: ws行为

来自分类Dev

程序未在EOF上停止

来自分类Dev

程序在 EOF 上打印垃圾

来自分类Dev

bash-嵌套的EOF

来自分类Dev

如何使用sudo,EOF和静默输出将cat或tee打击成文件

来自分类Dev

sudo cat << EOF>文件不起作用,sudo su起作用

来自分类Dev

在MySQL查询结果中检测EOF

来自分类Dev

在MySQL查询结果中检测EOF

来自分类Dev

shell bash,cat文件,同时使用sed替换文本

来自分类Dev

关于“猫<<-_EOF_”的shell脚本

来自分类Dev

Shell脚本中的后台进程收到EOF

来自分类Dev

关于“猫<<-_EOF_”的shell脚本

来自分类Dev

.somefile中的sh命令

来自分类Dev

'cat> some_file << EOF some_stuff EOF'和'echo“ some_stuff”> some_file'有什么区别?

来自分类Dev

bash脚本在获取时产生不同的结果