To separate regular commands in Unix is to put semicolon in the end like this:
cd /path/to/file;./someExecutable;
But it seems not working for commands like this:
./myProgram1 > /dev/null &
./myProgram2 > /dev/null &
=>./myProgram1 > /dev/null &;./myProgram2 > /dev/null &;
Is there any way separate these kind of commands?
Also, if are below 2 cases are equivalent if I copy paste to command prompt? Thanks.
cd /path/to/file;./someExecutable;
cd /path/to/file;
./someExecutable;
command1 & command2
Will executecommand1
, send the process to the background, and immediately begin executingcommand2
, even ifcommand1
has not completed.command1 ; command2
Will executecommand1
and then executecommand2
oncecommand1
finishes, regardless of whethercommand1
exited successfully.command1 && command2
will only executecommand2
oncecommand1
has completed execution successfully. Ifcommand1
fails,command2
will not execute.(...also, for completeness...)
command1 || command2
will only executecommand2
ifcommand1
fails (exits with a non-zero exit code.)Well the ";" makes the shell wait for the command to finish and then continues with the next command.
The "&" will send any process directly into the background and continues with the next command - no matter if the first command finished or is still running.
So "&;" will not work like you expect.
But actually I'm unsure what you expect.
Try this in your shell:
it will output: 2 1 3
Now compare it with
which will output 1 2 3
Regards.