Insert a newline in a perl -e statement?

5,064

Solution 1

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

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

Solution 2

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;
$y;'

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.

Solution 3

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

Solution 4

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";
$i=1;
$y=2;
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:
5,064

Related videos on Youtube

Francesco Novy
Author by

Francesco Novy

Updated on September 18, 2022

Comments

  • Francesco Novy
    Francesco Novy almost 2 years

    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:

    $x;
    $y;
    
    • Peter Mortensen
      Peter Mortensen over 14 years
      @lydonchandra: why do you want to insert a new line in the Perl one-liner?
  • quack quixote
    quack quixote over 14 years
    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
    quack quixote over 14 years
    i also thought of "here" documents but i can't make that work either.
  • Peter Mortensen
    Peter Mortensen over 14 years
    @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.
  • quack quixote
    quack quixote over 14 years
    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.
  • Mukul Kumar Sharma
    Mukul Kumar Sharma about 14 years
    How do I do those cool buttons like [Return]?
  • squircle
    squircle about 14 years
    @drewk: Just type <kbd>Ctrl</kbd>!