I'm new in Linux script and i face some commands and I didn't understand them.
cat <<- _EOF_
command1
command2
command3
_EOF_
Also,
less <<- _EOF
command1
command2
command3
_EOF
So, can any one explain them to me ?
I'm new in Linux script and i face some commands and I didn't understand them.
cat <<- _EOF_
command1
command2
command3
_EOF_
Also,
less <<- _EOF
command1
command2
command3
_EOF
So, can any one explain them to me ?
In program code, you have to mark which parts are meant to be commands and which are meant to be data. On typical type of data are strings, meaning sequences of characters (letters, digits etc.)
There are different ways to indicate string literals. The most common way is to quote the string data :
PATH="/home/user"
USER='joe'
But it can be hard to handle string literals that are longer and/or include line breaks:
MESSAGE="This is a relatively long string which isn't represented very well as a typical quoted string.\nAnd there are linebreaks in it."
For such cases, you would probably like to be able to say: "Treat the following as a string, until I tell you otherwise". And that's exactly what a here document does. It's a different way to signify a string literal. You define a marker that signifies the end of your text, then put your text, then the marker:
MESSAGE=<< EOF
Here be a long string.
With line breaks.
EOF
You're free to choose the marker, but it's typically something like EOF ("end of file"), eot ("end of text") or similar. You could have chosen "ABC" just the same
MESSAGE=<< ABC
Here be a long string.
With line breaks.
ABC
"Here documents", also known as "heredocs", "here-strings" or similar, have originated in Unix shells like bash
and can today be found in a lot of programming languages.
In shells, they work a bit differently, as they not represent strings as much as they represent files. So, for example
cat << EOF
One
EOF
is equivalent to
cat data.txt # where data.txt contains 'One'
The variant with <<-
instead of `<<' ignores leading whitespace in the heredoc data, so you can make your code more readable by identing the heredoc:
cat <<- EOF
One
Two
Three
EOF