9

I notice some sample bash for loops are spread out over multiple lines in examples

for VARIABLE in file1 file2 file3
do
    command1 on $VARIABLE
    command2
    commandN
done

(eg here http://www.cyberciti.biz/faq/bash-for-loop/) How do I enter a newline in the bash terminal (I use putty) ? When I press enter at the end of a line the system executes it.

  • 1
    Also in bash you can use the keyboard combination Ctrl + x + e to open the contents of the command prompt in a editor such as vim where you can work on it more easily, when you're done you can exit the editor and the command will run. – slm Jun 27 '13 at 0:22
  • Related answer: unix.stackexchange.com/questions/232912/… – Kusalananda Feb 15 '17 at 17:19
23

When you press Enter at the end of:

for VARIABLE in file1 file2 file3

The shell can't execute anything since that for loop is not finished. So instead, it will print a different prompt, the $PS2 prompt (generally >), until you enter the closing done.

However, after > is displayed, you can't go back to edit the first line.

Alternatively, instead of typing Enter, you can type Ctrl-VCtrl-J. That way, the newline character (aka ^J) is entered without the current buffer being accepted, and you can then go back to editing the first line later on.

In zsh, you can press Alt-Enter or EscEnter to insert a newline character without accepting the current buffer. To get the same behavior in bash, you can add to your ~/.inputrc:

"\e\C-m": "\026\n"

(\026 being the ^V character).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.