shell脚本记录

木头的喵喵拖孩

Here document 语法

参考
Here Document 也被称为 here-document/here-text/heredoc/hereis/here-string/here-script,在 Linux/Unix 中的 shell 中被广泛地应用,尤其在于用于传入多行分割参数给执行命令。除了 shell(包含 sh/csh/tcsh/ksh/bash/zsh 等),这种方式的功能也影响和很多其他语言诸如 Perl,PHP 以及 Ruby 等。这篇文章以 bash 为例进行使用说明。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash
# 打印多行参数

cat << EOF
This is a here document.
It can span multiple lines.
EOF

# 或者改个名字为CUSTOMEOF,只要结构一样,都是可以的

cat << CUSTOMEOF
This is a here document.
It can span multiple lines.
CUSTOMEOF

# 将内容写入文件
cat > test.txt << EOF
This is a here document.
It can span multiple lines.
EOF

  • 标题: shell脚本记录
  • 作者: 木头的喵喵拖孩
  • 创建于: 2023-12-06 15:40:15
  • 更新于: 2024-05-21 10:56:15
  • 链接: https://blog.xx-xx.top/2023/12/06/shell脚本记录/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
此页目录
shell脚本记录