Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using && after a heredoc in bash

Tags:

bash

heredoc

People also ask

What is the synonym of using?

The words employ and utilize are common synonyms of use. While all three words mean "to put into service especially to attain an end," use implies availing oneself of something as a means or instrument to an end. willing to use any means to achieve her ends.

What is the verb of using?

verb (used with object), used, us·ing. to employ for some purpose; put into service; make use of: to use a knife. to avail oneself of; apply to one's own purposes: to use the facilities. to expend or consume in use: We have used the money provided.

What is an example of use?

Use is the acting of employing or utilizing something or the intended purpose of something. An example of use is the act of hammering with a hammer and nails. An example of use is communication to the Internet.

Did you use to or used to?

It may be that many people in fact say use to rather than used to, but since the pronunciations are essentially identical, it makes no difference. (The same occurrence happens in the pronunciation of supposed to.) In writing, however, use to in place of used to is an error.


Chaining commands in a single line

You can put the control operator && right after the EOF word in your here document and you can chain more than one command:

cat > file <<-EOF && echo -n "hello " && echo world

It will wait for your here-document and then will print hello world.

Example

$ cat > file <<-EOF && echo -n "hello " && echo world
> a
> b
> EOF
hello world

$ cat file
a
b

Chaining commands after the heredoc delimiter

Now, if you want to place the following commands after the heredoc, you can group it in curly braces and continue chaining commands as follows:

echo -n "hello " && { cat > file <<-EOF
a
b
EOF
} && echo world

Example

$ echo -n "hello " && { cat > file <<-EOF
> a
> b
> EOF
> } && echo world
hello world

$ cat file
a
b

Using the set built in

If you're going to use set [-+]e instead of chained commands with &&, you must notice that surrounding a chunk of code with set -e and set +e is not a direct alternative and you must take care of the following:

Surrounding dependent commands with set [-+]e

echo first_command
false # it doesn't stop the execution of the script

# surrounded commands
set -e
echo successful_command_a
false # here stops the execution of the script
echo successful_command_b
set +e

# this command is never reached
echo last_command

As you can see, if you need to go on executing commands after the surrounded commands, this solution doesn't work.

Grouping Commands to the rescue

Instead, you can group the surrounded commands in order to create a subshell as follows:

echo first_command
false # it doesn't stop the execution of the script

# surrounded commands executed in a subshell
(
set -e
echo successful_command_a
false # here stops the execution of the group
echo successful_command_b
set +e # actually, this is not needed here
)

# the script is alive here
false # it doesn't stop the execution of the script
echo last_command

So, if you need to execute something else after your chained commands and you want to use the set builtin, consider the examples above.

Also notice the following about subshells:

Command substitution, commands grouped with parentheses, and asynchronous commands are invoked in a subshell environment that is a duplicate of the shell environment, except that traps caught by the shell are reset to the values that the shell inherited from its parent at invocation. Builtin commands that are invoked as part of a pipeline are also executed in a subshell environment. Changes made to the subshell environment cannot affect the shell’s execution environment.


If you are using the && operator just to stop at the command that fails and not continue, you might want to surround the chunk of code with set -e and close with set +e. that way you can remove the && and your code will most likely look cleaner.