Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Consider the following Perl one-liner:

perl -e '$x; $y;'

I'm executing this command in bash and I'd like to insert a newline (\n) between the ; and $y. I'd like to know how to do this in two similar but slightly different ways:

  1. How can I insert the newline in that position by pressing some meta key sequence to insert the newline?

  2. How can I insert the newline in that position by typing some non-meta character sequence? (eg, '$x;\n$y;')

In both situations I want the Perl interpreter to see an actual newline, like this:

share|improve this question
@lydonchandra: why do you want to insert a new line in the Perl one-liner? – Peter Mortensen Mar 24 '10 at 10:19
up vote 2 down vote accepted

Delete the space, then type CtrlV, CtrlJ.
Then Return.

The Ctrl-V prevents the shell interpreting the next character (newline) literally.

share|improve this answer
thanks.......... – portoalet Mar 24 '10 at 14:59
How do I do those cool buttons like [Return]? – drewk May 6 '10 at 2:32
@drewk: Just type <kbd>Ctrl</kbd>! – squircle May 6 '10 at 2:38

The question you have asked only concerns how the shell interprets its input and passes it along to other programs. It has nothing to do with Perl, per se.

This answer addresses typing new input to the shell. Use the others' answer of ^V^J to insert a newline into an existing command line while using command-line editing.

You should just be able to put this in a script:

foo '$x;

The argument given to the command will have a newline in the same place as it does in the script itself. You may need to take care to save such a script in such a way that uses Unix-style (LF-only) line breaks, otherwise you might get a CR+LF (DOS/Windows line breaks) or just CR (old Mac-style lineb reaks).

You can also do this at an interactive prompt, but you will see a continuation prompt before the second and any subsequent lines:

$ foo '$x;
> $y;'

In bash, you can also use the $'' quoting syntax to encode a newline character like this:

foo $'$x;\n$y;'

The argument passed to the program will be treated in a manner similar to an ANSI C string. If you want an actual backslash in the string you will have to escape it as \\, instead.

So, if you really want the literal-string quoting that single quotes give you, you should probably stick with an embedded newline so that you do not have to worry about extra escaping.

share|improve this answer

Move your cursor into position then press Ctrl-V Ctrl-J

share|improve this answer
Thanks......... – portoalet Mar 24 '10 at 15:00

If you want to have pico, vi or emacs editing, you can use the built-in fc command which stands for fix command.

You can use fc and Bash will open your last command in the defined editor, such as vi, emacs, etc. I use pico for this. You can then use multi-line editing very easily.

You can use line feeds and format to edit the command. Once you exit the editor, the command is saved to a Bash buffer and immediately executed.

In Bash, try this:

$ FCEDIT=pico
$ perl -e ''
$ fc

The command FCEDIT=pico just sets the editor to pico. Set any editor you want. You can export that and make permanent if you wish.

The command perl -e '' is an empty wrapper around your new one line perl script. When you type fc the last command is opened in the defined editor.

Now in Pico, go up to the '' and insert your Perl script. Remember that the script is not the same as if you were saving it into a text file and is subject to the same need to be aware of how the shell is going to interpret the input between the single quotes. Mainly, you will need to escape a single quote with \

In pico, insert whatever Perl code you want, (subject to the Bash interpretation of it...)

I did this:

perl -e  '
print "hello\n";
print "here is a second line\n";
print "\$i=$i, \$y=$y\n\n";

Now save the file to the default name. (In pico, Ctl+O is write out) and exit pico (Ctl+x). Bash will now echo then execute your command.

fc is documented in section 9.2 of the gnu manual HERE

share|improve this answer

It is almost possible. On the bash command-line, \ (backslash) can be used as line-continuation character. It's actually the escape character; it tells bash to treat the next character in the line as special.

You'd use this between command-line tokens to add newlines into the command-line. You must hit Return immediately after the \ for it to work at all:

#                      +-----Press return here!!
#                      V
user@host:$ cat foo | \
>           perl -e '$x; $y'

If you delete the single quote, ', (and optionally more characters) at the end of the repeated line,

perl -e '$x;\ $y

, and press Enter then more characters can be typed ending with the single quote, '. Note that $y is still active.

share|improve this answer
i can't make it work. it does allow me to enter the command on multiple lines, but then perl gets the newline and dies with a compilation error. that doesn't happen with the ctrl-V+ctrl-J solution above. you can use the backslash to get to a newline if you close the quote before the backslash and reopen on the new line: eg, perl -e '$x;'\[ENTER]'$y' ... that works because bash ignores the newline and concatenates the two quotes into one string. but that doesn't actually pass the newline to perl. – quack quixote Mar 25 '10 at 5:10
i also thought of "here" documents but i can't make that work either. – quack quixote Mar 25 '10 at 5:12
@quack quixote: thanks for the edit. I tested this solution while logged in over SSH with PuTTY (Windows) to an Ubuntu 9.1 based server. I also inserted print statements before and after the line-break in the Perl one-liner to verify that they were executed. But in any case the other solution is better. – Peter Mortensen Mar 25 '10 at 11:25
i think i'm just misunderstanding what you tried to explain. in your example the newline actually is after the "$y", isn't it? so it's not escaped with the backslash, and bash lets it happen because you haven't closed the quote yet. yeah, that works; i thought you were using the backslash to escape the newline, which is what didn't work for me. – quack quixote Mar 25 '10 at 12:11

You must log in to answer this question.

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