Bash – Why Combine Commands on a Single Line in a Bash Script

bashcommand linescripts

I am new to Linux and Bash scripting. At work, I've seen Bash scripts with constructions similar to this:

mkdir build && cd build && touch blank.txt

Or:

mkdir build; cd build; touch blank.txt

Or even the exotic:

COMMAND="mkdir build && cd build && touch blank.txt"
eval ${COMMAND}

The last example gives one possible use-case where a single line could be useful, but generally the following is easier to read and (at least for me) allows you to visually debug the script:

mkdir build
cd build
touch blank.txt

Are there technical advantages to cramming everything on a single line?

Best Answer

mkdir build && cd build && touch blank.txt

In Bash (and some other shells) && is a logical and, which means - if the previous command returns true the next command will be executed. There is also logical or ||. For example you can combine these two options in one statement:

mkdir /tmp/abc/123 && echo 'the dir is created' || echo "the dir isn't created"

Note, the construction cmd_1 && cmd_2 || comd_3 is not a substitute of the if; then; else statement, because no matter which of the preceding commands return false, the cmd_3 will be executed. So you must be careful about the circumstances in which you are using it. Here is an example:

$ true && false || echo success
success
$ false && true || echo success
success
$ false && false || echo success
success

As a rule rule of thumb, usually, when I'm using the cd command within a script, I'm putting a test whether the directory change is successful: cd somewhere/ || exit. More proper test is proposed by @dessert: if ! cd dir; then exit 1; fi. But in all cases as protection of script's failure it is better to use the set -e option as it is shown in the @mrks' answer.


mkdir build; cd build; touch blank.txt

; is a line delimiter and it is used when few separated commands are written in one line.


Note when ; or && and || are in use it is not mandatory to write the commands at on line - this is illustrated within the @allo's answer.

At all, IMO, there is not any special technical advantage/difference between writing the commands at one or at separate lines.


COMMAND="mkdir build && cd build && touch blank.txt"
eval ${COMMAND}

Here one or more commands (and their arguments) are grouped as value of a variable, and then this variable (argument) is converted to a command by the help of eval. Thus you will be able to change the command that will be executed multiple times within some script by changing it at only one place.

Let's say you need to change the way in which the file blank.txt is created, for example, you can change the relevant line in a way as this:

COMMAND="mkdir build && cd build && echo 'test' > blank.txt"

The actual eval's advantage over the usage of alias is when there re-directions, pipes, logical operators, etc. are in use.

In most cases you can use functions instead of eval when alias is not applicable. Also in case the variable contains only a single command, i.e. CMD="cat", we do not need eval, because Bash world-split will expand "$CMD" "$file1" "$file2" correctly.

Related Question