Bash – How to stop a bash shell expansion

bashbrace-expansioncommand linekillshell

I know some methods for stopping process. When I type:

echo {1..999999} > filename.txt 

I can't stop it from running. I can stop other processes with
Ctrl+C | Ctrl+D | Ctrl+\ and etc.
But none of them seem to be working with this command.

Some guys told me to simply close this terminal. Other than that, open new terminal kill all terminal processes, but I don't want to do it this way. In the server I won't have such chance to open new terminal, I think.

Can I open a new terminal session in text-mode?

Best Answer

The reason why cannot interrupt that with Ctrl-c etc... is that the shell isn't running any command at that point. It's busy expanding {1..999999} to compute what the command line arguments will eventually be once it gets to the point of running the command.

While external commands respond to termination signals like SIGINT (which is emitted by default when you press Ctrl-c), shells themselves ignore them. If they didn't, then, when you pressed Ctrl-c, then in addition to killing whatever command happened to be running, you'd also kill the shell itself! (This is not quite true because of tty job control and foreground and background process groups, but close enough for the purpose of that explanation.)

If you need to interrupt it, there is unfortunately nothing you can do but kill the shell itself. Killing the shell itself will cause your session to terminate. In that sense it's largely equivalent to closing the terminal window or terminating the SSH connection.

Related Question