How to go back to previous line of a script in terminal?

I am trying to write a script but I'm stuck here. See the code please!

aug@august:~/play$ for i in {1..100..4}
> do
> echo "august"
> touch august $i+$2
> mkdir dir

Is there any way to go previous line? I mean there was a mistype, I did in touch august $i+$2. Ipressed ENTER key, so I went for new line. How can I get back to previous line?


Solution 1:

bash's command-line editor doesn't support this, either do as anwar suggests or keep everything on one line separated by semicolons or put it into a script file.

On a side note, zsh has features that help with this. The zsh line editor (zle) is a more fully featured editor and supports moving within a multi-line command. When the command becomes too long to handle in zle it has a builtin feature, edit-command-line, which opens up the command-line in your favorite editor. It's usually not bound, try with:

bindkey "^[e" edit-command-line

Alt-e should now activate it, save and quit to go back to the command-line.

Solution 2:

After you pressed Enter and got an error message, you can just go back in history:

Just press the key in your keyboard to fix the typo. If you press key the terminal will show you this line:

for i in {1..100..4}; do echo "august"; touch august $i+$2; mkdir;

Correct the mistyped stuff around touch august $i+$2 and add a done at the end of the command.

Then hit the Enter key. This should solve your problem.