how to insert newline in a already existing BASH string on the command line how to insert newline in a already existing BASH string on the command line bash bash

how to insert newline in a already existing BASH string on the command line


You can use two shortcuts to do that ctrl + k and ctrl + y:

echo "some command" && echo "some other long command"

Now move cursor somewhere (in my example, cursor is marked by >):

echo "some command" && > echo "some other command"

Now press ctrl + k - this will cut everything after a cursor:

echo "some command" && >

Now put \ (backslash) and press enter:

echo "some command" && \>

And now paste the part you've previously cut by ctrl + y:

echo "some command" && \echo "some other long command"

Edit: to move more easily around in a long command, you can use shortcuts:

  • alt + b - move one word backwards (on Mac OS X: ESC + b)
  • alt + f - move one word forwards (on Mac OS X: ESC + f)

Ultra-solution

You can also open current line in a editor using Ctrl-x + Ctrl-e (two shortcuts, one after another). Then edit it just as a regular text file, save & quit and voila, edited command will execute.

If you want to choose which editor to use, just set EDITOR environment variable.


You can create text file for script. For example:

test.sh

#!/bin/bashecho Hello, world!

So you will need to execute this:

chmod +x test.sh./test.sh