here document and double backslash

2019-02-23 23:43发布

If I use a here document in a shell script that contains multiple backslashes '\\', the shell translates it into a single backslash. Can I work around this without changing the text ?

$ cat <<EOF
> Print \\hello \\world
> EOF
Print \hello \world

标签: bash shell
2条回答
不美不萌又怎样
2楼-- · 2019-02-24 00:01

As an alternative to what Dennis mentions The command sed can also take care of this.

sed 's/\\/\\\\/g' <<EOF
Print \\hello \\world
EOF
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-02-24 00:06

Quote the beginning here document marker:

cat <<'EOF'
Print \\hello \\world
EOF
查看更多
登录 后发表回答