Search code examples
bashdefinitionheredoccommand-substitution

Bash - is this called a definition?


I'm pretty new to bash,

I learned about using variables like this:

var=$(cat << EOF
this is echoed
and this as well...
EOF
)

echo "$var"

if the above code is called using a variable how do you call the approach below? is this called a using a definition?

def="This should be echoed\nand this as well...\n"

printf "$def"

I'm asking about the right terminology of the second approach.


Solution

  • Both var and def are variables; that is, they are the names of string-data in your current environment, accessed using $ as in your examples.

    To create each variable, you assign a string to it. This is called an assignment. Syntactically, = is the assignment operator.

    Once var and def are assigned, there is no difference in how they store data; the stored data in each case is merely a string.

    There is nothing "special" requiring additional terminology in the assignment of def. However, there are two interesting aspects of the var assignment:

    • As alluded to in a comment, your use of cat is a pattern called a "here document".
    • To create and capture the "document" (which is just a string), you are using $(), which is a form of "command substitution".