Analytics Vidhya
Published in

Analytics Vidhya

What is heredoc in shell ?

A heredoc is a way to provide input to a command , by just writing it along the command , as in :

$ cat <<delm
> hello world
> delm
hello world

A heredoc is not about providing options or arguments to a command . The command must actually be able to read from standard input , so for example writing the earlier example while using the echo command instead of cat , will just print a new line , as echo does not read from standard input .

$ echo <<in
> Hello world
> in

In a heredoc interpolation or expansion takes place , for example ,

$ X=1
$ cat <<in
> $X
> in
1

This can be prevented , by quoting the first delimiter in a heredoc string , using either ' or " or \ , as in :

cat <<'in'
> $X
> in
$X
cat <<\in
> $X
> in
$X
cat <<"in"
> $X
> in
$X

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store