Today I opened gnome-terminal
and I wrote
ls && sleep 4 && gnome-terminal
to open another terminal after the completion of ls
command and waiting for 4 seconds.
So it successfully opened a new terminal after previous commands completely ran (including sleep 4
).
After that, next time I typed a new command
ls -lR && sleep 4 && gnome-terminal
The command ls -lR
completed after 3 seconds, but after that none of the commands sleep 4
and gnome-terminal
ran successfully.
What is the problem?
&&
means to run it if the previous command was successful. In Unix that generally means exit status 0.In the first instance,
ls
did not succeed, so it exited with a non-zero exit status, and bash did not run the second command.In the second example, I ls'd a existing file, so ls exited with 0 as exit status, and the command was executed.
If you want to run commands unconditionally, e.g. not dependent on the result of the first, you may separate them with
;
like thisand so forth.
Thus you may do
In addition to
&&
and;
you have||
which is the opposite of&&
: Only run the command if the previous command failed with a non-zero exit status. If the first command succeeds, the next will not be executed. If it fails, the next will be executed.So in short:
&&
: Run if preceding command exited with 0;
: Run unconditionally||
: Run if preceding command exited with a non-zero exit status.&
: Run both commands in paralell, the first in background and second in foreground.The command
ls -lR
exited with an exit-status different than zero, so the following commands have not been executed. Most probablyls
was unable to open a subdirectory. It didn't happen in your first command because you didn't use the-R
-option.From
man bash
:From
man ls
: