mkdir build && cd build && touch blank.txt
In Bash (and some other shells and most high level programming languages) &&
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 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 one 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 are re-directions, pipes, logical operators - at all control flow operators - 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 word-splitting will expand "$CMD" "$file1" "$file2"
correctly.
&&
is not the same as;
.