Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Possible Duplicate:
insert newline in perl -e statement

Hi

If I do this in bash

perl -e '$x; $y'

Is there any character that can behave like a new line?

i.e. I want to do

perl -e '$x; some_chars_as_new_line $y'

where some_chars_as_new_line might be \, not meta character.

where perl interpreter actually sees an actual new line.

$x; 
$y;

Many thanks

share|improve this question

marked as duplicate by John T, Josh K, quack quixote, Dennis Williamson, fretje Mar 25 '10 at 11:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2  
Duplicate of superuser.com/questions/123467/… –  Josh K Mar 25 '10 at 4:40
    
please don't double-post. you can edit your earlier question to add information if you need to. –  quack quixote Mar 25 '10 at 4:44
    
this is a slightly different question to superuser.com/questions/123467/… (which has been answered) Please don't close. –  portoalet Mar 25 '10 at 5:26
    
ok, yes, i see a miniscule difference. but there's an answer on your other question (that i just expanded) that answers this question too. (granted, not as well as Chris's answer below...) since they are so closely related (they have practically the same title), i still think they should be in one post. –  quack quixote Mar 25 '10 at 5:34
add comment

1 Answer

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.

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 it ends with 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
    
@Chris: this question will be closed as a duplicate; please post this to the duplicate and delete it here. (you'll lose the downvote when you do!) –  quack quixote Mar 25 '10 at 5:23
    
@quack: I am not so concerned with the downvote that I would copy this answer to the other question as it stands (anyway, it might be downvoted there, too). I would want to see the other questioned explicitly broadened to include an ‘initial input’ sense (along with the original ‘edit/insert’ sense). But my editor rep is too fresh for me to initiate such a rewrite without the OPs consent. @lydonchandra, IMO, these questions are close enough they could be merged. I could do it if you like (just say “go ahead, merge them”). You could easily revert the changes if you did not like the result. –  Chris Johnsen Mar 25 '10 at 11:19
    
i've gone ahead and edited the other question to include both parts, so this answer is now on-topic there. if you don't want to repost, it can be moved by the mods at some point in the future. –  quack quixote Mar 25 '10 at 12:18
add comment

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