19

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

Hello
World

but the output I am getting is

Hello\nWorld

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

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

2
  • 3
    aliasing echo like you want is a bad practice IMHO
    – shellholic
    Mar 11, 2011 at 21:02
  • 1
    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, 2011 at 1:13

4 Answers 4

27

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'
1
  • shopt -s xpg_echo; Worked for me.
    – Bashuser
    Mar 14, 2011 at 13:21
8

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

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

printf '%s\n' "Hello\nWorld"
1
  • +1 printf is a little more complicated to use than echo, but it more than pays off in avoiding echo's inconsistencies. Mar 11, 2011 at 22:23
6

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

When you use . myfile.sh, 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.)

0
4

This works fine in terminal

#!/bin/bash
alias echo="echo -e"
echo "Hello\nWorld"

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

run as ./your_file

6
  • run it as bash your_file and check the output.
    – Bashuser
    Mar 11, 2011 at 21:06
  • 1
    Works as expected
    – LaLeX
    Mar 11, 2011 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, 2011 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, 2011 at 21:25
  • 1
    Also if you don't want to search and replace each instance of echo in your script put alias echo="echo -e" at the top of your script and run it as ". your_file" instead of "./your_file"
    – LaLeX
    Mar 11, 2011 at 21:36

You must log in to answer this question.

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