cat in scripts

1 Antwort [Letzter Beitrag]
Sam Witters
Offline
Beigetreten: 11/01/2021

HOSTS_BLOCKLISTS=$(cat <<'__EOT'
### Local File Example
# file:/path/to/local/file
### Bad Hosts List
URL
URL
URL
URL
URL
__EOT
)

Hi. Why many scripts feature cat in a way shown in example above? Cat is for concatenating files, or is it?

Magic Banana

I am a member!

Offline
Beigetreten: 07/24/2010

cat can concatenate one single file, i.e., output it. That use is often unnecessarily: most commands can take files in argument and, if they cannot (or if they behave differently when processing the standard input), the standard input can be redirected with <. In the example you show, however, cat reads a here document that becomes the content of a variable. The here document can contain anything, including both single and double quotes. To define such a variable, here documents (and, as a consequence, cat) are useful.