Run another command when previous command completes [duplicate]
Clash Royale CLAN TAG#URR8PPP
This question already has an answer here:
Is there a difference between the '&&' and ';' symbols in a standard BASH terminal?
6 answers
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?
command-line bash syntax
marked as duplicate by karel, Charles Green, Dan, dessert
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 29 at 18:33
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Is there a difference between the '&&' and ';' symbols in a standard BASH terminal?
6 answers
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?
command-line bash syntax
marked as duplicate by karel, Charles Green, Dan, dessert
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 29 at 18:33
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Is there a difference between the '&&' and ';' symbols in a standard BASH terminal?
6 answers
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?
command-line bash syntax
This question already has an answer here:
Is there a difference between the '&&' and ';' symbols in a standard BASH terminal?
6 answers
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?
This question already has an answer here:
Is there a difference between the '&&' and ';' symbols in a standard BASH terminal?
6 answers
command-line bash syntax
command-line bash syntax
edited Jan 28 at 19:46
wjandrea
9,20442363
9,20442363
asked Jan 28 at 16:34
Abhishek KamalAbhishek Kamal
13910
13910
marked as duplicate by karel, Charles Green, Dan, dessert
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 29 at 18:33
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by karel, Charles Green, Dan, dessert
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 29 at 18:33
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
&&
means to run it if the previous command was successful. In Unix that generally means exit status 0.
$ ls barfoofba && echo "This will not be echo'd"
ls: cannot access 'barfoofba': No such file or directory
$ ls bar && echo "This will be echo'd"
This will be echo'd
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 this
command1; command2; command3
and so forth.
Thus you may do
ls -lR ; sleep 4 ; gnome-terminal
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.
6
I think of it like 0 means SUCCESS which is like true.
– Jason Goemaat
Jan 29 at 0:00
4
@KodosJohnson In the C world, 0 means success because non-zero means an error, and there could be hundreds of error codes (seeerror.h
). Happy return codes are all alike...
– chrylis
Jan 29 at 2:48
3
@AbhishekKamal No, I can't try on my machine. ls -lR should work, but it might fail if it for instance hits one directory it doesn't have permissions to look in. For instance willls -lR /
always fail...
– vidarlo
Jan 29 at 5:53
2
@JonBentley The "echo" in "echo'd" is the name of a command, not an English verb, even if it happens to be spelled the same, so it's somewhat open to question how it should be conjugated. This is more obvious if you pick other commands: what would the past tense ofls
be, for instance?
– IMSoP
Jan 29 at 15:29
2
@chrylis: No, the point is exactly that "0 for true" is not how it is "in the C world". In the C programming language, conditionals (and the logical operators) consider 0 to be false and everything else to be true. The convention for process exit codes in the shell is exactly the opposite. -- Even though both C and the shell are both parts of a larger Unix tradition.
– Henning Makholm
Jan 29 at 16:55
|
show 4 more comments
The command ls -lR
exited with an exit-status different than zero, so the following commands have not been executed. Most probably ls
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
:
command1 && command2
command2 is executed if, and only if, command1 returns an exit status
of zero.
From man ls
:
Exit status:
0 if OK,
1 if minor problems (e.g., cannot access subdirectory)
2 if serious trouble (e.g., cannot access command-line argument).
Thanks @mook765, Your answer is perfect according to my question.. I cleared my confusion
– Abhishek Kamal
Jan 29 at 1:50
According to your answer, when i runls -lR && sleep 4 && gnome-terminal
withsudo
, it successfully runs.
– Abhishek Kamal
Jan 29 at 1:52
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
&&
means to run it if the previous command was successful. In Unix that generally means exit status 0.
$ ls barfoofba && echo "This will not be echo'd"
ls: cannot access 'barfoofba': No such file or directory
$ ls bar && echo "This will be echo'd"
This will be echo'd
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 this
command1; command2; command3
and so forth.
Thus you may do
ls -lR ; sleep 4 ; gnome-terminal
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.
6
I think of it like 0 means SUCCESS which is like true.
– Jason Goemaat
Jan 29 at 0:00
4
@KodosJohnson In the C world, 0 means success because non-zero means an error, and there could be hundreds of error codes (seeerror.h
). Happy return codes are all alike...
– chrylis
Jan 29 at 2:48
3
@AbhishekKamal No, I can't try on my machine. ls -lR should work, but it might fail if it for instance hits one directory it doesn't have permissions to look in. For instance willls -lR /
always fail...
– vidarlo
Jan 29 at 5:53
2
@JonBentley The "echo" in "echo'd" is the name of a command, not an English verb, even if it happens to be spelled the same, so it's somewhat open to question how it should be conjugated. This is more obvious if you pick other commands: what would the past tense ofls
be, for instance?
– IMSoP
Jan 29 at 15:29
2
@chrylis: No, the point is exactly that "0 for true" is not how it is "in the C world". In the C programming language, conditionals (and the logical operators) consider 0 to be false and everything else to be true. The convention for process exit codes in the shell is exactly the opposite. -- Even though both C and the shell are both parts of a larger Unix tradition.
– Henning Makholm
Jan 29 at 16:55
|
show 4 more comments
&&
means to run it if the previous command was successful. In Unix that generally means exit status 0.
$ ls barfoofba && echo "This will not be echo'd"
ls: cannot access 'barfoofba': No such file or directory
$ ls bar && echo "This will be echo'd"
This will be echo'd
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 this
command1; command2; command3
and so forth.
Thus you may do
ls -lR ; sleep 4 ; gnome-terminal
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.
6
I think of it like 0 means SUCCESS which is like true.
– Jason Goemaat
Jan 29 at 0:00
4
@KodosJohnson In the C world, 0 means success because non-zero means an error, and there could be hundreds of error codes (seeerror.h
). Happy return codes are all alike...
– chrylis
Jan 29 at 2:48
3
@AbhishekKamal No, I can't try on my machine. ls -lR should work, but it might fail if it for instance hits one directory it doesn't have permissions to look in. For instance willls -lR /
always fail...
– vidarlo
Jan 29 at 5:53
2
@JonBentley The "echo" in "echo'd" is the name of a command, not an English verb, even if it happens to be spelled the same, so it's somewhat open to question how it should be conjugated. This is more obvious if you pick other commands: what would the past tense ofls
be, for instance?
– IMSoP
Jan 29 at 15:29
2
@chrylis: No, the point is exactly that "0 for true" is not how it is "in the C world". In the C programming language, conditionals (and the logical operators) consider 0 to be false and everything else to be true. The convention for process exit codes in the shell is exactly the opposite. -- Even though both C and the shell are both parts of a larger Unix tradition.
– Henning Makholm
Jan 29 at 16:55
|
show 4 more comments
&&
means to run it if the previous command was successful. In Unix that generally means exit status 0.
$ ls barfoofba && echo "This will not be echo'd"
ls: cannot access 'barfoofba': No such file or directory
$ ls bar && echo "This will be echo'd"
This will be echo'd
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 this
command1; command2; command3
and so forth.
Thus you may do
ls -lR ; sleep 4 ; gnome-terminal
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.
&&
means to run it if the previous command was successful. In Unix that generally means exit status 0.
$ ls barfoofba && echo "This will not be echo'd"
ls: cannot access 'barfoofba': No such file or directory
$ ls bar && echo "This will be echo'd"
This will be echo'd
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 this
command1; command2; command3
and so forth.
Thus you may do
ls -lR ; sleep 4 ; gnome-terminal
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.
edited Jan 29 at 16:58
answered Jan 28 at 16:46
vidarlovidarlo
10.5k52547
10.5k52547
6
I think of it like 0 means SUCCESS which is like true.
– Jason Goemaat
Jan 29 at 0:00
4
@KodosJohnson In the C world, 0 means success because non-zero means an error, and there could be hundreds of error codes (seeerror.h
). Happy return codes are all alike...
– chrylis
Jan 29 at 2:48
3
@AbhishekKamal No, I can't try on my machine. ls -lR should work, but it might fail if it for instance hits one directory it doesn't have permissions to look in. For instance willls -lR /
always fail...
– vidarlo
Jan 29 at 5:53
2
@JonBentley The "echo" in "echo'd" is the name of a command, not an English verb, even if it happens to be spelled the same, so it's somewhat open to question how it should be conjugated. This is more obvious if you pick other commands: what would the past tense ofls
be, for instance?
– IMSoP
Jan 29 at 15:29
2
@chrylis: No, the point is exactly that "0 for true" is not how it is "in the C world". In the C programming language, conditionals (and the logical operators) consider 0 to be false and everything else to be true. The convention for process exit codes in the shell is exactly the opposite. -- Even though both C and the shell are both parts of a larger Unix tradition.
– Henning Makholm
Jan 29 at 16:55
|
show 4 more comments
6
I think of it like 0 means SUCCESS which is like true.
– Jason Goemaat
Jan 29 at 0:00
4
@KodosJohnson In the C world, 0 means success because non-zero means an error, and there could be hundreds of error codes (seeerror.h
). Happy return codes are all alike...
– chrylis
Jan 29 at 2:48
3
@AbhishekKamal No, I can't try on my machine. ls -lR should work, but it might fail if it for instance hits one directory it doesn't have permissions to look in. For instance willls -lR /
always fail...
– vidarlo
Jan 29 at 5:53
2
@JonBentley The "echo" in "echo'd" is the name of a command, not an English verb, even if it happens to be spelled the same, so it's somewhat open to question how it should be conjugated. This is more obvious if you pick other commands: what would the past tense ofls
be, for instance?
– IMSoP
Jan 29 at 15:29
2
@chrylis: No, the point is exactly that "0 for true" is not how it is "in the C world". In the C programming language, conditionals (and the logical operators) consider 0 to be false and everything else to be true. The convention for process exit codes in the shell is exactly the opposite. -- Even though both C and the shell are both parts of a larger Unix tradition.
– Henning Makholm
Jan 29 at 16:55
6
6
I think of it like 0 means SUCCESS which is like true.
– Jason Goemaat
Jan 29 at 0:00
I think of it like 0 means SUCCESS which is like true.
– Jason Goemaat
Jan 29 at 0:00
4
4
@KodosJohnson In the C world, 0 means success because non-zero means an error, and there could be hundreds of error codes (see
error.h
). Happy return codes are all alike...– chrylis
Jan 29 at 2:48
@KodosJohnson In the C world, 0 means success because non-zero means an error, and there could be hundreds of error codes (see
error.h
). Happy return codes are all alike...– chrylis
Jan 29 at 2:48
3
3
@AbhishekKamal No, I can't try on my machine. ls -lR should work, but it might fail if it for instance hits one directory it doesn't have permissions to look in. For instance will
ls -lR /
always fail...– vidarlo
Jan 29 at 5:53
@AbhishekKamal No, I can't try on my machine. ls -lR should work, but it might fail if it for instance hits one directory it doesn't have permissions to look in. For instance will
ls -lR /
always fail...– vidarlo
Jan 29 at 5:53
2
2
@JonBentley The "echo" in "echo'd" is the name of a command, not an English verb, even if it happens to be spelled the same, so it's somewhat open to question how it should be conjugated. This is more obvious if you pick other commands: what would the past tense of
ls
be, for instance?– IMSoP
Jan 29 at 15:29
@JonBentley The "echo" in "echo'd" is the name of a command, not an English verb, even if it happens to be spelled the same, so it's somewhat open to question how it should be conjugated. This is more obvious if you pick other commands: what would the past tense of
ls
be, for instance?– IMSoP
Jan 29 at 15:29
2
2
@chrylis: No, the point is exactly that "0 for true" is not how it is "in the C world". In the C programming language, conditionals (and the logical operators) consider 0 to be false and everything else to be true. The convention for process exit codes in the shell is exactly the opposite. -- Even though both C and the shell are both parts of a larger Unix tradition.
– Henning Makholm
Jan 29 at 16:55
@chrylis: No, the point is exactly that "0 for true" is not how it is "in the C world". In the C programming language, conditionals (and the logical operators) consider 0 to be false and everything else to be true. The convention for process exit codes in the shell is exactly the opposite. -- Even though both C and the shell are both parts of a larger Unix tradition.
– Henning Makholm
Jan 29 at 16:55
|
show 4 more comments
The command ls -lR
exited with an exit-status different than zero, so the following commands have not been executed. Most probably ls
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
:
command1 && command2
command2 is executed if, and only if, command1 returns an exit status
of zero.
From man ls
:
Exit status:
0 if OK,
1 if minor problems (e.g., cannot access subdirectory)
2 if serious trouble (e.g., cannot access command-line argument).
Thanks @mook765, Your answer is perfect according to my question.. I cleared my confusion
– Abhishek Kamal
Jan 29 at 1:50
According to your answer, when i runls -lR && sleep 4 && gnome-terminal
withsudo
, it successfully runs.
– Abhishek Kamal
Jan 29 at 1:52
add a comment |
The command ls -lR
exited with an exit-status different than zero, so the following commands have not been executed. Most probably ls
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
:
command1 && command2
command2 is executed if, and only if, command1 returns an exit status
of zero.
From man ls
:
Exit status:
0 if OK,
1 if minor problems (e.g., cannot access subdirectory)
2 if serious trouble (e.g., cannot access command-line argument).
Thanks @mook765, Your answer is perfect according to my question.. I cleared my confusion
– Abhishek Kamal
Jan 29 at 1:50
According to your answer, when i runls -lR && sleep 4 && gnome-terminal
withsudo
, it successfully runs.
– Abhishek Kamal
Jan 29 at 1:52
add a comment |
The command ls -lR
exited with an exit-status different than zero, so the following commands have not been executed. Most probably ls
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
:
command1 && command2
command2 is executed if, and only if, command1 returns an exit status
of zero.
From man ls
:
Exit status:
0 if OK,
1 if minor problems (e.g., cannot access subdirectory)
2 if serious trouble (e.g., cannot access command-line argument).
The command ls -lR
exited with an exit-status different than zero, so the following commands have not been executed. Most probably ls
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
:
command1 && command2
command2 is executed if, and only if, command1 returns an exit status
of zero.
From man ls
:
Exit status:
0 if OK,
1 if minor problems (e.g., cannot access subdirectory)
2 if serious trouble (e.g., cannot access command-line argument).
answered Jan 28 at 16:51
mook765mook765
4,16421332
4,16421332
Thanks @mook765, Your answer is perfect according to my question.. I cleared my confusion
– Abhishek Kamal
Jan 29 at 1:50
According to your answer, when i runls -lR && sleep 4 && gnome-terminal
withsudo
, it successfully runs.
– Abhishek Kamal
Jan 29 at 1:52
add a comment |
Thanks @mook765, Your answer is perfect according to my question.. I cleared my confusion
– Abhishek Kamal
Jan 29 at 1:50
According to your answer, when i runls -lR && sleep 4 && gnome-terminal
withsudo
, it successfully runs.
– Abhishek Kamal
Jan 29 at 1:52
Thanks @mook765, Your answer is perfect according to my question.. I cleared my confusion
– Abhishek Kamal
Jan 29 at 1:50
Thanks @mook765, Your answer is perfect according to my question.. I cleared my confusion
– Abhishek Kamal
Jan 29 at 1:50
According to your answer, when i run
ls -lR && sleep 4 && gnome-terminal
with sudo
, it successfully runs.– Abhishek Kamal
Jan 29 at 1:52
According to your answer, when i run
ls -lR && sleep 4 && gnome-terminal
with sudo
, it successfully runs.– Abhishek Kamal
Jan 29 at 1:52
add a comment |