Heredocs
Heredocs allow you to pass formatted strings in shell.
The syntax looks like the following:
[COMMAND] <<[-] 'DELIMITER'
HERE-DOCUMENT
DELIMITERUsing a - causes all leading tab characters to be ignored. This allows you to use indentation when writing heredocs.
If you quote the delimiter, the shell will not substitute variables.
cat << EOF
The current working directory is: $PWD
You are logged in as: $(whoami)
EOFThe output might be:
The current working directory is: /home/takashiidobe
You are logged in as: takashiidobeYou can redirect the output too, with << and >:
cat << EOF > file.txt
The current working directory is: $PWD
You are logged in as: $(whoami)
EOFThere heredoc can be piped as well:
cat <<'EOF' | sed 's/l/e/g'
Hello
World
EOFThe output might look like:
Heeeo
Wored