If you want an alternative that can be stopped with ctrl+c, the following works in bash:
i=1; while [ $i -le 999999 ]; do echo $((i++)) >> filename.txt; done
Be sure to use the redirection symbol1 >>
to append to the file instead of overwrite it.
Also, I seem to be able to stop it with ctrl+c when running the command within a subshell using parenthesis ( echo {1..999999} > numero.txt )
Note that the output file is never created for the reasons Celada explained.
See Subshells - Advanced Bash-Scripting Guide
To further support Celada's answer, below is the order in which the bash shell's fork-and-exec scheme operates. You can see that the command is not executed until after shell expansions are performed; no fork is yet created. In the command echo {1..999999} > filename.txt
, the parent shell is the process in which the shell expansion is being performed. ctrl+c, of course, doesn't kill the parent shell.
From Bash Guide for Beginners
1.4.1.1. Shell syntax
If input is not commented, the shell reads it and divides it into
words and operators, employing quoting rules to define the meaning of
each character of input. Then these words and operators are translated
into commands and other constructs, which return an exit status
available for inspection or processing. The above fork-and-exec scheme
is only applied after the shell has analyzed input in the following
way:
The shell reads its input from a file, from a string or from the
user's terminal.
Input is broken up into words and operators, obeying the quoting
rules, see Chapter 3. These tokens are separated by metacharacters.
Alias expansion is performed.
The shell parses (analyzes and substitutes) the tokens into simple and
compound commands.
Bash performs various shell expansions, breaking the expanded tokens
into lists of filenames and commands and arguments.
Redirection is performed if necessary, redirection operators and their
operands are removed from the argument list.
Commands are executed.
Optionally the shell waits for the command to complete and collects
its exit status.
From Bash Guide for Beginners 3.4.1. General
After the command has been split into tokens (see Section 1.4.1.1),
these tokens or words are expanded or resolved. There are eight kinds
of expansion performed, which we will discuss in the next sections, in
the order that they are expanded.
Brace expansion is the first among the order of expansions performed.
Stopping a Shell Expansion
You can actually stop a shell expansion by sending kill -SIGSTOP <pid>
to the pid of the shell. echo $$
will give the pid of the current shell or you can try pgrep -l bash
and try to determine which shell is running the expansion.
The expansion can be resumed where it left off with kill -SIGCONT <pid>
.
I don't know of any way to kill the expansion aside from killing the parent shell or letting it finish. To me, this makes a good case for running expansions in a subshell.
ctrl-z
(forgot already how to make that buttons). Or from another shell, kill the shell with-9
. – ott-- Mar 14 '15 at 22:18