DEV Community

Discussion on: Bash Brackets Quick Reference

Collapse
 
ikirker profile image
Ian Kirker

A couple of things about HEREDOC bits:

  • HEREDOCs when used with a command are fed into stdin for a command. The way you've used it in your first example for assignment to a variable doesn't work, at least for me and my bash 4.4.19.
$ nice_message=<<MESSAGE
Hi there!  I really like the way you look
when you are teaching newbies things
with empathy and compassion!
You rock!
MESSAGE
$ echo $nice_message

$
$ cat <<MESSAGE
Hi there!  I really like the way you look
when you are teaching newbies things
with empathy and compassion!
You rock!
MESSAGE
Hi there!  I really like the way you look
when you are teaching newbies things
with empathy and compassion!
You rock!
$ 
  • If you put single quotes around the initial tag, the HEREDOC will be treated like a single-quoted string, leaving variable expressions uninterpolated.
$ cat <<EOF
$RANDOM
EOF
4578

$ cat <<'EOF'
$RANDOM
EOF
$RANDOM
  • You can also use triple less-than symbols without a tag to just feed in a single simple string, like so:
$ cat <<EOF
beep
EOF
beep

$ cat <<<beep
beep
$
Collapse
 
rpalo profile image
Ryan Palo

I updated the post with your help and extra info! Thanks again!

Collapse
 
rpalo profile image
Ryan Palo

Hm. I’ll take a look at that. Thanks! I was doing all of my local testing in Zsh, and forgot that there might be differences.

Some comments have been hidden by the post's author - find out more