[英]Bash tries to execute commands in heredoc
I am trying to write a simple bash script that will print a multiline output to another file. 我正在尝试编写一个简单的bash脚本,它将多行输出打印到另一个文件。 I am doing it through heredoc
format: 我是通过heredoc
格式来做的:
#!/bin/sh
echo "Hello!"
cat <<EOF > ~/Desktop/what.txt
a=`echo $1 | awk -F. '{print $NF}'`
b=`echo $2 | tr '[:upper:]' '[:lower:]'`
EOF
I was expecting to see a file in my desktop with these contents: 我期待在桌面上看到包含以下内容的文件:
a=`echo $1 | awk -F. '{print $NF}'`
b=`echo $2 | tr '[:upper:]' '[:lower:]'`
But instead, I am seeing these as the contents of my what.txt
file: 但相反,我将这些视为what.txt
文件的内容:
a=
b=
Somehow, even though it is part of a heredoc
, bash is trying to execute it line by line. 不知何故,即使它是heredoc
一部分,bash也试图逐行执行它。 How do I prevent this, and print the contents to the file as it is? 如何防止这种情况,并将内容按原样打印到文件中?
Quote EOF
so that bash
takes inputs literally: 引用EOF
以便bash
字面输入:
cat <<'EOF' > what.txt
a=`echo $1 | awk -F. '{print $NF}'`
b=`echo $2 | tr '[:upper:]' '[:lower:]'`
EOF
Also start using $()
for command substitution instead of old and problematic ``. 也开始使用$()
进行命令替换而不是旧的和有问题的``。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.