Syntax
[cmd] <<[-] delimeter [cmd]
contents
delimeter
All contents will be passed to the cmd as an input, examples below will use EOF as a delimeter and cat as a command, you can change to whatever you want.
With Variable
cat <<EOF
echo "$HOME"
EOF
result:
echo "/home/clavinjune"
Escape Variable
Use \$ instead of $ to escape specific variable
cat <<EOF
echo "$HOME"
echo "\$HOME"
EOF
result:
echo "/home/clavinjune"
echo "$HOME"
Escape All Variables
Use 'EOF' instead of EOF to escape all variables
cat <<'EOF'
echo "$HOME"
echo "\$HOME"
EOF
result:
echo "$HOME"
echo "\$HOME"
Remove Leading Tab
Use <<- instead of << to remove leading tabs
cat <<-EOF
echo "$HOME"
echo "\$HOME"
EOF
result:
echo "/home/clavinjune"
echo "$HOME"
Add More Pipeline
cat <<EOF | grep june
echo "$HOME"
echo "\$HOME"
EOF
result:
echo "/home/clavinjune"
Write To a File
cat <<-'EOF' > /tmp/foo
echo "$HOME"
echo "\$HOME"
EOF
result:
$ cat /tmp/foo
echo "$HOME"
echo "\$HOME"
Thank you for reading!
Top comments (1)
so nice, thanks for sharing. expect to update~