0

I know that to symbolize a new line, I would do echo -e "line 1 \nline 2" > text.txt. I find that using \n repeatedly can be annoying, and I would to know if there a way to actually go down a line in the terminal without it executing commands. Like if I were to edit python in the terminal I could write

def main():
    print "hello world"

And pressing "enter" to go to a new line wouldn't have executed def main(): , it just went to a new line. Is there anything similar to this in the normal terminal?

2 Answers 2

0

If you just want to echo text as in your example, you can just use the Enter key in place of \n like this:

echo "line 1
line 2" > text.txt

For that to work, you have to surround your text with "" as shown or with ''.

If you want to enter a sequence of commands without executing them until you're done, you can begin the sequence with a { and end it with a }, like this:

{
echo line 1
echo line 2
date
}

You can also redirect the output of the whole sequence by following the closing } with your redirection, as

{
echo line 1
echo line 2
date
} > text.txt
4
  • I want to be able to do what you did in the first example, but every time I hit enter it executes the command Oct 9, 2012 at 0:52
  • 1
    @WillumMaguire: If you typed echo "line 1 with only one " and hit Enter, I would expect you to see the secondary prompt > . If not that, then an error message about an unterminated string. What exactly are you typing that is executed when you hit Enter?
    – garyjohn
    Oct 9, 2012 at 1:41
  • I just tried this: echo "line 1 I also tried using E0F, but I can't seem to get it to work correctly Oct 9, 2012 at 3:09
  • @WillumMaguire: I'm sorry, but I am unable to explain the behavior you are seeing. I tried a few different shells but always saw one of the two results I mentioned above. I also skimmed the bash(1) man page but did not see any options that would cause that behavior.
    – garyjohn
    Oct 9, 2012 at 4:05
0

In addition to the unmatched quotation mark, you can use heredocs:

$ python <<EOF
> def main():
>       print "Hello"
> 
> main()
> EOF
Hello
$

Note that EOF is simply a common and convenient delineator, any token will do.

You must log in to answer this question.

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