Gatlin.io Icon
Image of Austin Gatlin's face
February 28, 2022

heredoc

<< END ... END is heredoc syntax. It allows for multiline inputs. END is an arbitrary delimiter to tell the shell when to start and stop reading text. It can be anything. E.g., END, EOF, DELIMIT, etc.

cat << DELIMIT
I am text input
across multiple lines
wow, very impressive
DELIMIT

You can write those inputs to a file.

cat << DELIMIT > ~/.bash_aliases
  ...
DELIMIT

Removing tabs from HEREDOC text

This will not work for spaces, only tabs

cat <<-EOF
  hello
  world
EOF
hello
world

Preventing variable expansion in HEREDOCs

HEREDOCs will expand variables by default. To prevent this, wrap the opening delimiter in quotes.

var="hey"

cat << DELIMIT
$var
DELIMIT
hey

cat << 'DELIMIT'
$var
DELIMIT
$var