How can I cancel the rest of a list of commands in Bash?
Clash Royale CLAN TAG#URR8PPP
up vote
10
down vote
favorite
In Bash, occasionally I will type in a list of commands and hit Enter, and only later realize that there is a mistake with some command near the end of the list. I know that if I press Ctrl+C it will terminate the currently running command and cancel the rest of the list. Is there any way to cancel the rest of the list without terminating the currently running command?
For example, let's say I have typed something like
foo; bar
or
foo && bar
where foo
is a long-running command that it is very important not to interrupt, and bar
does something irreversible and unwanted (say, shutdown -h now
or rm -rf /
). While foo
is still running, is there a general way of telling the shell to let foo
finish but not to run bar
afterwards? (Yes, I could change the permissions on bar
so that it's not executable, but that's not particularly convenient if bar
is something like rm
that I want to use in the meantime, nor will it work if I don't own bar
or if bar
is a builtin.)
bash command-line job-control
add a comment |Â
up vote
10
down vote
favorite
In Bash, occasionally I will type in a list of commands and hit Enter, and only later realize that there is a mistake with some command near the end of the list. I know that if I press Ctrl+C it will terminate the currently running command and cancel the rest of the list. Is there any way to cancel the rest of the list without terminating the currently running command?
For example, let's say I have typed something like
foo; bar
or
foo && bar
where foo
is a long-running command that it is very important not to interrupt, and bar
does something irreversible and unwanted (say, shutdown -h now
or rm -rf /
). While foo
is still running, is there a general way of telling the shell to let foo
finish but not to run bar
afterwards? (Yes, I could change the permissions on bar
so that it's not executable, but that's not particularly convenient if bar
is something like rm
that I want to use in the meantime, nor will it work if I don't own bar
or if bar
is a builtin.)
bash command-line job-control
You can modify the lastbar
command tosome_command
:^bar^some_command
before to be executed.
â GAD3R
Apr 2 at 9:34
3
@GAD3R: but this will not modify current line, it will start a new one.
â Arkadiusz Drabczyk
Apr 2 at 9:37
add a comment |Â
up vote
10
down vote
favorite
up vote
10
down vote
favorite
In Bash, occasionally I will type in a list of commands and hit Enter, and only later realize that there is a mistake with some command near the end of the list. I know that if I press Ctrl+C it will terminate the currently running command and cancel the rest of the list. Is there any way to cancel the rest of the list without terminating the currently running command?
For example, let's say I have typed something like
foo; bar
or
foo && bar
where foo
is a long-running command that it is very important not to interrupt, and bar
does something irreversible and unwanted (say, shutdown -h now
or rm -rf /
). While foo
is still running, is there a general way of telling the shell to let foo
finish but not to run bar
afterwards? (Yes, I could change the permissions on bar
so that it's not executable, but that's not particularly convenient if bar
is something like rm
that I want to use in the meantime, nor will it work if I don't own bar
or if bar
is a builtin.)
bash command-line job-control
In Bash, occasionally I will type in a list of commands and hit Enter, and only later realize that there is a mistake with some command near the end of the list. I know that if I press Ctrl+C it will terminate the currently running command and cancel the rest of the list. Is there any way to cancel the rest of the list without terminating the currently running command?
For example, let's say I have typed something like
foo; bar
or
foo && bar
where foo
is a long-running command that it is very important not to interrupt, and bar
does something irreversible and unwanted (say, shutdown -h now
or rm -rf /
). While foo
is still running, is there a general way of telling the shell to let foo
finish but not to run bar
afterwards? (Yes, I could change the permissions on bar
so that it's not executable, but that's not particularly convenient if bar
is something like rm
that I want to use in the meantime, nor will it work if I don't own bar
or if bar
is a builtin.)
bash command-line job-control
asked Apr 2 at 8:06
Psychonaut
286211
286211
You can modify the lastbar
command tosome_command
:^bar^some_command
before to be executed.
â GAD3R
Apr 2 at 9:34
3
@GAD3R: but this will not modify current line, it will start a new one.
â Arkadiusz Drabczyk
Apr 2 at 9:37
add a comment |Â
You can modify the lastbar
command tosome_command
:^bar^some_command
before to be executed.
â GAD3R
Apr 2 at 9:34
3
@GAD3R: but this will not modify current line, it will start a new one.
â Arkadiusz Drabczyk
Apr 2 at 9:37
You can modify the last
bar
command to some_command
: ^bar^some_command
before to be executed.â GAD3R
Apr 2 at 9:34
You can modify the last
bar
command to some_command
: ^bar^some_command
before to be executed.â GAD3R
Apr 2 at 9:34
3
3
@GAD3R: but this will not modify current line, it will start a new one.
â Arkadiusz Drabczyk
Apr 2 at 9:37
@GAD3R: but this will not modify current line, it will start a new one.
â Arkadiusz Drabczyk
Apr 2 at 9:37
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
I've observed that using CtrlZ to shift the program to a background process does the trick.
foo && bar
Thanks to @Arkadiusz Drabczyk for pointing it in comments that foo; bar
doesn't give control in the required way.
Then:
^Z
[1]+ Stopped foo
The command stops only the first task and
fg %1
This brings only task foo
to the foreground and completes the task and exits.
PS: This can be checked with two scripts writing to a file. The first one sleeping for a few seconds to give time to be put back.
I'm lost on why the CtrlZ handles only the command running and leaves the rest. Would love to get to know.
1
I cannot reproduce it. Whatbash
version do you use? I useGNU bash, version 4.3.46(1)-release (x86_64-slackware-linux-gnu)
. I wrote 2 bash scripts that write to different files:write1.sh
: pastebin.com/rbKmdWgB andwrite2.sh
: pastebin.com/bNx3VRws. I run them like that:./write1.sh ; ./write2.sh
. The first script echoeswrote 1
for a few times, I pressC-z
, it says[1]+ Stopped ./write1.sh
and I can immediately see output of the second script:wrote 2
being echoed repeatedly.
â Arkadiusz Drabczyk
Apr 2 at 20:46
2
I think the reason that this method works withcommand1 && command2
pipelines is thatControl-z
sendsSIGCHLD
signal to the process. It can be checked withecho $?
.Bash
then sees that the first process didn't finish successfully and does not execute next processes.
â Arkadiusz Drabczyk
Apr 2 at 21:32
I am usingGNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
. Yeah, I think you are right.foo; bar
cannot be controlled in the desired way. But we can do the required stop of the second process withCtrl+Z
if the command run isfoo && bar
. I will update the answer. Thank you for pointing it out.
â Revanth Chetluru
Apr 3 at 5:46
@ArkadiuszDrabczyk doesn't Ctrl-Z send SIGTSTP?
â muru
Apr 3 at 5:55
@RevanthChetluru note thatbar
is still suspended and shows up injobs
output, so it probably should be killed too.
â muru
Apr 3 at 6:00
 |Â
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
I've observed that using CtrlZ to shift the program to a background process does the trick.
foo && bar
Thanks to @Arkadiusz Drabczyk for pointing it in comments that foo; bar
doesn't give control in the required way.
Then:
^Z
[1]+ Stopped foo
The command stops only the first task and
fg %1
This brings only task foo
to the foreground and completes the task and exits.
PS: This can be checked with two scripts writing to a file. The first one sleeping for a few seconds to give time to be put back.
I'm lost on why the CtrlZ handles only the command running and leaves the rest. Would love to get to know.
1
I cannot reproduce it. Whatbash
version do you use? I useGNU bash, version 4.3.46(1)-release (x86_64-slackware-linux-gnu)
. I wrote 2 bash scripts that write to different files:write1.sh
: pastebin.com/rbKmdWgB andwrite2.sh
: pastebin.com/bNx3VRws. I run them like that:./write1.sh ; ./write2.sh
. The first script echoeswrote 1
for a few times, I pressC-z
, it says[1]+ Stopped ./write1.sh
and I can immediately see output of the second script:wrote 2
being echoed repeatedly.
â Arkadiusz Drabczyk
Apr 2 at 20:46
2
I think the reason that this method works withcommand1 && command2
pipelines is thatControl-z
sendsSIGCHLD
signal to the process. It can be checked withecho $?
.Bash
then sees that the first process didn't finish successfully and does not execute next processes.
â Arkadiusz Drabczyk
Apr 2 at 21:32
I am usingGNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
. Yeah, I think you are right.foo; bar
cannot be controlled in the desired way. But we can do the required stop of the second process withCtrl+Z
if the command run isfoo && bar
. I will update the answer. Thank you for pointing it out.
â Revanth Chetluru
Apr 3 at 5:46
@ArkadiuszDrabczyk doesn't Ctrl-Z send SIGTSTP?
â muru
Apr 3 at 5:55
@RevanthChetluru note thatbar
is still suspended and shows up injobs
output, so it probably should be killed too.
â muru
Apr 3 at 6:00
 |Â
show 2 more comments
up vote
3
down vote
I've observed that using CtrlZ to shift the program to a background process does the trick.
foo && bar
Thanks to @Arkadiusz Drabczyk for pointing it in comments that foo; bar
doesn't give control in the required way.
Then:
^Z
[1]+ Stopped foo
The command stops only the first task and
fg %1
This brings only task foo
to the foreground and completes the task and exits.
PS: This can be checked with two scripts writing to a file. The first one sleeping for a few seconds to give time to be put back.
I'm lost on why the CtrlZ handles only the command running and leaves the rest. Would love to get to know.
1
I cannot reproduce it. Whatbash
version do you use? I useGNU bash, version 4.3.46(1)-release (x86_64-slackware-linux-gnu)
. I wrote 2 bash scripts that write to different files:write1.sh
: pastebin.com/rbKmdWgB andwrite2.sh
: pastebin.com/bNx3VRws. I run them like that:./write1.sh ; ./write2.sh
. The first script echoeswrote 1
for a few times, I pressC-z
, it says[1]+ Stopped ./write1.sh
and I can immediately see output of the second script:wrote 2
being echoed repeatedly.
â Arkadiusz Drabczyk
Apr 2 at 20:46
2
I think the reason that this method works withcommand1 && command2
pipelines is thatControl-z
sendsSIGCHLD
signal to the process. It can be checked withecho $?
.Bash
then sees that the first process didn't finish successfully and does not execute next processes.
â Arkadiusz Drabczyk
Apr 2 at 21:32
I am usingGNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
. Yeah, I think you are right.foo; bar
cannot be controlled in the desired way. But we can do the required stop of the second process withCtrl+Z
if the command run isfoo && bar
. I will update the answer. Thank you for pointing it out.
â Revanth Chetluru
Apr 3 at 5:46
@ArkadiuszDrabczyk doesn't Ctrl-Z send SIGTSTP?
â muru
Apr 3 at 5:55
@RevanthChetluru note thatbar
is still suspended and shows up injobs
output, so it probably should be killed too.
â muru
Apr 3 at 6:00
 |Â
show 2 more comments
up vote
3
down vote
up vote
3
down vote
I've observed that using CtrlZ to shift the program to a background process does the trick.
foo && bar
Thanks to @Arkadiusz Drabczyk for pointing it in comments that foo; bar
doesn't give control in the required way.
Then:
^Z
[1]+ Stopped foo
The command stops only the first task and
fg %1
This brings only task foo
to the foreground and completes the task and exits.
PS: This can be checked with two scripts writing to a file. The first one sleeping for a few seconds to give time to be put back.
I'm lost on why the CtrlZ handles only the command running and leaves the rest. Would love to get to know.
I've observed that using CtrlZ to shift the program to a background process does the trick.
foo && bar
Thanks to @Arkadiusz Drabczyk for pointing it in comments that foo; bar
doesn't give control in the required way.
Then:
^Z
[1]+ Stopped foo
The command stops only the first task and
fg %1
This brings only task foo
to the foreground and completes the task and exits.
PS: This can be checked with two scripts writing to a file. The first one sleeping for a few seconds to give time to be put back.
I'm lost on why the CtrlZ handles only the command running and leaves the rest. Would love to get to know.
edited Apr 3 at 5:56
muru
33.3k576141
33.3k576141
answered Apr 2 at 18:16
Revanth Chetluru
315
315
1
I cannot reproduce it. Whatbash
version do you use? I useGNU bash, version 4.3.46(1)-release (x86_64-slackware-linux-gnu)
. I wrote 2 bash scripts that write to different files:write1.sh
: pastebin.com/rbKmdWgB andwrite2.sh
: pastebin.com/bNx3VRws. I run them like that:./write1.sh ; ./write2.sh
. The first script echoeswrote 1
for a few times, I pressC-z
, it says[1]+ Stopped ./write1.sh
and I can immediately see output of the second script:wrote 2
being echoed repeatedly.
â Arkadiusz Drabczyk
Apr 2 at 20:46
2
I think the reason that this method works withcommand1 && command2
pipelines is thatControl-z
sendsSIGCHLD
signal to the process. It can be checked withecho $?
.Bash
then sees that the first process didn't finish successfully and does not execute next processes.
â Arkadiusz Drabczyk
Apr 2 at 21:32
I am usingGNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
. Yeah, I think you are right.foo; bar
cannot be controlled in the desired way. But we can do the required stop of the second process withCtrl+Z
if the command run isfoo && bar
. I will update the answer. Thank you for pointing it out.
â Revanth Chetluru
Apr 3 at 5:46
@ArkadiuszDrabczyk doesn't Ctrl-Z send SIGTSTP?
â muru
Apr 3 at 5:55
@RevanthChetluru note thatbar
is still suspended and shows up injobs
output, so it probably should be killed too.
â muru
Apr 3 at 6:00
 |Â
show 2 more comments
1
I cannot reproduce it. Whatbash
version do you use? I useGNU bash, version 4.3.46(1)-release (x86_64-slackware-linux-gnu)
. I wrote 2 bash scripts that write to different files:write1.sh
: pastebin.com/rbKmdWgB andwrite2.sh
: pastebin.com/bNx3VRws. I run them like that:./write1.sh ; ./write2.sh
. The first script echoeswrote 1
for a few times, I pressC-z
, it says[1]+ Stopped ./write1.sh
and I can immediately see output of the second script:wrote 2
being echoed repeatedly.
â Arkadiusz Drabczyk
Apr 2 at 20:46
2
I think the reason that this method works withcommand1 && command2
pipelines is thatControl-z
sendsSIGCHLD
signal to the process. It can be checked withecho $?
.Bash
then sees that the first process didn't finish successfully and does not execute next processes.
â Arkadiusz Drabczyk
Apr 2 at 21:32
I am usingGNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
. Yeah, I think you are right.foo; bar
cannot be controlled in the desired way. But we can do the required stop of the second process withCtrl+Z
if the command run isfoo && bar
. I will update the answer. Thank you for pointing it out.
â Revanth Chetluru
Apr 3 at 5:46
@ArkadiuszDrabczyk doesn't Ctrl-Z send SIGTSTP?
â muru
Apr 3 at 5:55
@RevanthChetluru note thatbar
is still suspended and shows up injobs
output, so it probably should be killed too.
â muru
Apr 3 at 6:00
1
1
I cannot reproduce it. What
bash
version do you use? I use GNU bash, version 4.3.46(1)-release (x86_64-slackware-linux-gnu)
. I wrote 2 bash scripts that write to different files: write1.sh
: pastebin.com/rbKmdWgB and write2.sh
: pastebin.com/bNx3VRws. I run them like that: ./write1.sh ; ./write2.sh
. The first script echoes wrote 1
for a few times, I press C-z
, it says [1]+ Stopped ./write1.sh
and I can immediately see output of the second script: wrote 2
being echoed repeatedly.â Arkadiusz Drabczyk
Apr 2 at 20:46
I cannot reproduce it. What
bash
version do you use? I use GNU bash, version 4.3.46(1)-release (x86_64-slackware-linux-gnu)
. I wrote 2 bash scripts that write to different files: write1.sh
: pastebin.com/rbKmdWgB and write2.sh
: pastebin.com/bNx3VRws. I run them like that: ./write1.sh ; ./write2.sh
. The first script echoes wrote 1
for a few times, I press C-z
, it says [1]+ Stopped ./write1.sh
and I can immediately see output of the second script: wrote 2
being echoed repeatedly.â Arkadiusz Drabczyk
Apr 2 at 20:46
2
2
I think the reason that this method works with
command1 && command2
pipelines is that Control-z
sends SIGCHLD
signal to the process. It can be checked with echo $?
. Bash
then sees that the first process didn't finish successfully and does not execute next processes.â Arkadiusz Drabczyk
Apr 2 at 21:32
I think the reason that this method works with
command1 && command2
pipelines is that Control-z
sends SIGCHLD
signal to the process. It can be checked with echo $?
. Bash
then sees that the first process didn't finish successfully and does not execute next processes.â Arkadiusz Drabczyk
Apr 2 at 21:32
I am using
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
. Yeah, I think you are right. foo; bar
cannot be controlled in the desired way. But we can do the required stop of the second process with Ctrl+Z
if the command run is foo && bar
. I will update the answer. Thank you for pointing it out.â Revanth Chetluru
Apr 3 at 5:46
I am using
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
. Yeah, I think you are right. foo; bar
cannot be controlled in the desired way. But we can do the required stop of the second process with Ctrl+Z
if the command run is foo && bar
. I will update the answer. Thank you for pointing it out.â Revanth Chetluru
Apr 3 at 5:46
@ArkadiuszDrabczyk doesn't Ctrl-Z send SIGTSTP?
â muru
Apr 3 at 5:55
@ArkadiuszDrabczyk doesn't Ctrl-Z send SIGTSTP?
â muru
Apr 3 at 5:55
@RevanthChetluru note that
bar
is still suspended and shows up in jobs
output, so it probably should be killed too.â muru
Apr 3 at 6:00
@RevanthChetluru note that
bar
is still suspended and shows up in jobs
output, so it probably should be killed too.â muru
Apr 3 at 6:00
 |Â
show 2 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f434991%2fhow-can-i-cancel-the-rest-of-a-list-of-commands-in-bash%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
You can modify the last
bar
command tosome_command
:^bar^some_command
before to be executed.â GAD3R
Apr 2 at 9:34
3
@GAD3R: but this will not modify current line, it will start a new one.
â Arkadiusz Drabczyk
Apr 2 at 9:37