Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

I have bash script which has lots of echo statements and also I aliased echo to echo -e both in .bash_profile and .bashrc, so that new lines are printed properly for a statement like echo 'Hello\nWorld' the output should be


but the output I am getting is


I even tried using shopt -s expand_aliases in the script, it doesn't help

I am running my script as bash /scripts/; if I run it as . /scripts/ I am getting the desired output...

share|improve this question
aliasing echo like you want is a bad practice IMHO – shellholic Mar 11 '11 at 21:02
Use double quotes to that the \n gets interpreted. Single quotes prevent the interpretation of the \n and a bunch of other things. – BillThor Mar 12 '11 at 1:13
up vote 14 down vote accepted

The mixed history of echo means its default operation varies from shell to shell. POSIX specifies that the result of echo is “implementation-defined” if the first argument is -n or any argument contains a backslash.

It is more reliable to use printf (either as a built-in command or an external command) where the behavior is more well defined: the C-style backslash escapes and format specifiers are supported in the format string (the first argument).

printf 'foo\nbar\n'
printf '%s\n%s\n' foo bar

You can control the expansion of backslash escape sequences of bash’s echo built-in command with the xpg_echo shell option.

Set it at the top of any bash script to make echo automatically expand backslash escapes without having to add -e to every echo command.

shopt -s xpg_echo
echo 'foo\nbar'
share|improve this answer
shopt -s xpg_echo; Worked for me. – Bashuser Mar 14 '11 at 13:21

The recommended practice is to use printf for all new scripts.

printf '%s\n%s\n' "Hello" "World"

printf '%s\n' "Hello\nWorld"
share|improve this answer
Thanks a lot for your answer – Bashuser Mar 11 '11 at 21:44
+1 printf is a little more complicated to use than echo, but it more than pays off in avoiding echo's inconsistencies. – Gordon Davisson Mar 11 '11 at 22:23

When you use bash, Bash is ran in "batch" mode, on a separate process, and does not read its profile or rcfile.

When you use ., the file is sourced by the current shell process (as if its contents were typed by you), therefore it sees your currently defined aliases.

In general, it is a Very Bad Idea to write scripts that depend on any particular shell configuration, especially aliases, unless you define them in the script itself. (Never rely on user's .bashrc, even if it's your own.)

share|improve this answer
Thanks a lot for your answer – Bashuser Mar 11 '11 at 21:43

This works fine in terminal

alias echo="echo -e"
echo "Hello\nWorld"

save to a file and make it exeutable (chmod +x) it

run as ./your_file

share|improve this answer
run it as bash your_file and check the output. – Bashuser Mar 11 '11 at 21:06
Works as expected – LaLeX Mar 11 '11 at 21:08
but echo "Hello\nWorld" is not showing the expected output. As echo is already aliased to echo -e it should but that is not working and thats my question – Bashuser Mar 11 '11 at 21:20
Since it's bad practice to alias echo (programs that depend on it may break) you should remove the alias and in your script search for echo end replace it by echo -e – LaLeX Mar 11 '11 at 21:25
Thanks a lot LaLex for your answers, is there a way that I can achieve the expected result with the alias though it is not a good practice – Bashuser Mar 11 '11 at 21:36

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.