Bash – How to input / start a new line in bash terminal

bashline-editor

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.

Best Answer

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).

Related Question