9

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?

1
  • you should write script in a file then run it from terminal. You can also use UP key for previous command, but no way to revert the command.
    – shantanu
    Sep 8, 2012 at 7:40

4 Answers 4

2

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.

1

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.

1

as explained here, in bash you have Commands for Manipulating the History as stated in it's manual. One of them is edit-and-execute-command.

This will open your editor with your commands already typed in a temporary file and you can correct previous lines easily. You can achieve this in emacs mode by C-xC-e, and vi mode by pressing v in normal mode.

edit-and-execute-command (C-xC-e)

Invoke an editor on the current command line, and execute the result as shell commands. Bash attempts to invoke $VISUAL, $EDITOR, and emacs as the editor, in that order.

1
  • Wow. Years of linux, and I just know this.
    – azzamsa
    Aug 30, 2019 at 0:55
0

This is what worked for me.

CTRL + C from the current command. Then press the . Now you can edit whichever line you want by moving around with the arrow buttons - or .

Good Luck...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .