Title here
Summary here
Using the -e
option can create simple multi-line text.
echo -e 'aa\nbb'
aa
bb
Here Document (abbreviated as Heredoc) is a way of input redirection in Shell, allowing you to embed multi-line text within scripts or commands and pass it to commands or files.
command <<EOF
Multi-line text content
EOF
command
:such as cat
, sed
, gawk
.<<EOF
:marks the beginning of the document and defines the end marker.EOF
:marks the end position of the document.Here, EOF
is just an example, and any marker can be used.
Multi-line text can be redirected to the foo.txt
file.
cat <<EOF > foo.txt
apple
banana
EOF
cat foo.txt
apple
banana
grep 'app' <<EOF
apple
banana
EOF
If the EOF marker is used with quotes, the function will be different.
Quote Type | Variable/Special Character |
---|---|
EOF | Interpret |
'EOF' | Do Not Interpret |
"EOF" | Do Not Interpret |
If the content is just a pure document, try to use quotes to reduce the impact of special characters.
Single/double quotes are the same, and neither will interpret the $
symbol.
cat <<'EOF'
$HOME
EOF
$HOME
And not using quotes is the same, and will interpret the $
symbol.
cat <<EOF
$HOME
EOF
/home/kuga