Here documents and here strings
Execute command with here document
Section titled “Execute command with here document”ssh -p 21 example@example.com <<EOF echo 'printing pwd' echo "\$(pwd)" ls -a find '*.txt'EOF$ is escaped because we do not want it to be expanded by the current shell i.e $(pwd) is to be executed on the remote shell.
Another way:
ssh -p 21 example@example.com <<'EOF' echo 'printing pwd' echo "$(pwd)" ls -a find '*.txt'EOFNote: The closing EOF should be at the beginning of the line (No whitespaces before). If indentation is required, tabs may be used if you start your heredoc with <<-. See the Indenting here documents and Limit Strings examples for more information.
Indenting here documents
Section titled “Indenting here documents”You can indent the text inside here documents with tabs, you need to use the <<- redirection operator instead of <<:
$ cat <<- EOF This is some content indented with tabs `\t`. You cannot indent with spaces you __have__ to use tabs. Bash will remove empty space before these lines. __Note__: Be sure to replace spaces with tabs when copying this example.EOF
This is some content indented with tabs _\t_.You cannot indent with spaces you __have__ to use tabs.Bash will remove empty space before these lines.__Note__: Be sure to replace spaces with tabs when copying this example.One practical use case of this (as mentioned in man bash)
is in shell scripts, for example:
if cond; then cat <<- EOF hello there EOFfiIt is customary to indent the lines within code blocks as in this if statement, for better readability.
Without the <<- operator syntax, we would be forced to write the above code like this:
if cond; then cat << EOFhellothereEOFfiThat’s very unpleasant to read, and it gets much worse in a more complex realistic script.
Here strings
Section titled “Here strings”You can feed a command using here strings like this:
$ awk '{print $2}' <<< "hello world - how are you?"world
$ awk '{print $1}' <<< "hello how are you> she is fine"hellosheYou can also feed a while loop with a here string:
$ while IFS=" " read -r word1 word2 rest> do> echo "$word1"> done <<< "hello how are you - i am fine"helloCreate a file
Section titled “Create a file”A classic use of here documents is to create a file by typing its content:
cat > fruits.txt << EOFappleorangelemonEOFThe here-document is the lines between the << EOF and EOF.
This here document becomes the input of the cat command.
The cat command simply outputs its input,
and using the output redirection operator > we redirect to a file fruits.txt.
As a result, the fruits.txt file will contain the lines:
appleorangelemonThe usual rules of output redirection apply:
if fruits.txt did not exist before, it will be created.
If it existed before, it will be truncated.
Run several commands with sudo
Section titled “Run several commands with sudo”sudo -s <<EOF a='var' echo 'Running serveral commands with sudo' mktemp -d echo "\$a"EOF$aneeds to be escaped to prevent it to be expanded by the current shell
Or
sudo -s <<'EOF' a='var' echo 'Running serveral commands with sudo' mktemp -d echo "$a"EOFLimit Strings
Section titled “Limit Strings”A heredoc uses the limitstring to determine when to stop consuming input. The terminating limitstring must
- Be at the start of a line.
Correct:
cat <<limitstringline 1line 2limitstringThis will output:
line 1line 2Incorrect use:
cat <<limitstringline 1line 2 limitstringSince limitstring on the last line is not exactly at the start of the line, the shell will continue to wait for further input, until it sees a line that starts with limitstring and doesn’t contain anything else. Only then it will stop waiting for input, and proceed to pass the here-document to the cat command.
Note that when you prefix the initial limitstring with a hyphen, any tabs at the start of the line are removed before parsing, so the data and the limit string can be indented with tabs (for ease of reading in shell scripts).
cat <<-limitstring line 1 has a tab each before the words line and has line 2 has two leading tabs limitstringwill produce
line 1 has a tab each before the words line and hasline 2 has two leading tabswith the leading tabs (but not the internal tabs) removed.