How can I cancel the rest of a list of commands in Bash?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
10
down vote

favorite
4












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.)







share|improve this question




















  • You can modify the last bar command to some_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















up vote
10
down vote

favorite
4












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.)







share|improve this question




















  • You can modify the last bar command to some_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













up vote
10
down vote

favorite
4









up vote
10
down vote

favorite
4






4





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.)







share|improve this question












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.)









share|improve this question











share|improve this question




share|improve this question










asked Apr 2 at 8:06









Psychonaut

286211




286211











  • You can modify the last bar command to some_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







  • 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











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.






share|improve this answer


















  • 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






  • 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 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










  • @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










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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.






share|improve this answer


















  • 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






  • 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 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










  • @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














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.






share|improve this answer


















  • 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






  • 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 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










  • @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












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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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. 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




    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











  • @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












  • 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






  • 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 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










  • @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







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












 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?