4 ms·
Lots of good refreshers, the cat + heredoc combo is powerful but lots of cryptically subtle variations in behavior between: <<EOF (expands variables etc) <<'E
by metadat 4y ago
Lots of good refreshers, the cat + heredoc combo is powerful but lots of cryptically subtle variations in behavior between:
<<EOF (expands variables etc)
<<'EOF' (doesn't expand variables etc)
<<-EOF (trims leading tabs)
Am I missing any more of the tricks?
- dotopotoro 4y agoTrimming version does not trim whitespace indentation. I guess point goes to “tabs” camp.
- metadat 4y agoCorrect, though you could pipe to sed and trim leading spaces with relative ease: sed 's/^ *//' <<EOF Which I'd prefer anyways because it's less cryptic. I try to stay away from the obscure, rarely used notations because I'll always have to look it up in StackOverflow anytime I come back to it. These can be confusingly sharp edges.
- 0xbadcafebee 4y agohttps://linux.die.net/man/1/bash https://linux.die.net/man/1/bash "Here Documents" and "Here Strings" sections.
- alganet 4y agoYou can do it in compound commands as well: while read -r LINE do printf %s "$LINE" done <<FOO foo bar baz FOO This is faster than using cat (one less path lookup, one less fork, one less exec, read is builtin), works on all shells. There is a <<-'EOF' as well (trims left tabs and does not expand vars). Another cool trick that only bash and zsh have is `printf -v NAME`, which prints to a variable instead of stdout. You can use it to avoid subshells (one less fork for each subshell avoided).