Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

How can I enter the following contents in a file:


I tried using echo "Hi\nabcd" >> ab.txt, but in the file it's written as is (the \n is included, instead of a newline)

share|improve this question

4 Answers 4

You need to tell echo to honor escape sequences.

echo -e "Hi\nabcd" >> ab.txt
share|improve this answer
thanks ..thats what I was looking for ... – Novice User Apr 2 '12 at 15:44

The behavior of echo varies from shell to shell¹; printf's behavior is more standard.

printf "Hi\nabcd" >> ab.txt

¹ "It is not possible to use echo portably across all POSIX systems unless both -n (as the first argument) and escape sequences are omitted."

share|improve this answer
Best answer, IMO. – jw013 Apr 2 '12 at 17:56
Yup. I've been bitten when echo's behavior changed in a system update, and some of my scripts broke... – Gordon Davisson Apr 2 '12 at 22:59
type printf printf is a shell builtin - it's the same situation as with echo: in bash it is a builtin. – user unknown Apr 3 '12 at 13:51
@userunknown: You may be missing the point. printf "foo\nbar\n" will work on every system, shell builtin or not, while echo -e "foo\nbar" definitely won't. – user112553 Apr 4 '12 at 14:35
@user112553: My point is, that a builtin might be implemented differently, but I don't know every shell, so maybe all behave the same for println, but not for echo. – user unknown Apr 4 '12 at 14:40

You can also just do cat > file, then type away and hit Ctrl-D when you're done.

share|improve this answer

You can also play games with quoting:

 % echo 'Hi
> abcd' >> ab.txt

You type that in bash by hitting "Enter" after "Hi". Bash uses '>' as its continuation-of-command prompt for me. I could not escape an individual return with a backslash for some reason.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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