echo doesn't work right

Hi,when I run my first shell script,I got something that doesn't work right.
I wrote this code in the script.

echo -e "Hello,World\a\n"

But the screen print like this:

-e Hello,World

The "-e" wasn't supposed to be printed out.
Can anyone help me out?:wall:
Many thanks!:slight_smile:

What shell are you using?
(csh, ksh, bash, etc.)

Type:

man echo

See if "-e" is part of your "echo" command.

bash
I typed

#!/bin/bash

in the script

---------- Post updated at 11:49 AM ---------- Previous update was at 11:43 AM ----------

Yes, in the description it says
-e enable interpretation of backslash escapes
And when I type the command directly in the shell,it work right!
So weird!

[LEFT]More importantly which version of echo are you using

$  echo -e "cheeses \n\tCashel Blue\n\tRoquefort"
cheeses
        Cashel Blue
        Roquefort
$ which echo
/bin/echo
$ rpm -qf /bin/echo
coreutils-5.2.1-36.el4

In your shell and your script try

which echo

[/LEFT]

Mate, try

echo $SHELL

this will give which shell you are in.

Also Try

which bash

this will give the address for bash like "/usr/bin/bash"

add this to the first line of your script with shebang(#!).

In Bash "-e" option with echo should work.

Both of the command return /bin/bash

The reason I suggested

which echo

was to see if $PATH is different in your shell and your script. (eg you have a PATH setting in your shell which isn't exported to your script) if the command above returns different values for both then call the command in the script using the full path name returned in the shell, where it worked.

I try that and it turn out to return /bin/echo both

Plus,when I type

/bin/echo -e "Hello,world\a\n"

in the script it work right
Can you tell me why ?

What version of bash do you have? bash --version

Why are you even using echo -e?

Use printf instead.

Hi, Demon:

Amen to what scottn said. You'll see echo in many scripts so you should be familiar with it, but echo with any options is not portable. Do yourself a favor and learn your way around the printf(1) command (usually a shell-builtin).

Regarding your problem, perhaps your environment has aliased echo or has defined a function that overrides the builtin. In bash, the following command would indicate exactly what your shell is trying to execute (whether you type it at the command line or in a script):

type echo

Regards,
Alister

---------- Post updated at 04:05 PM ---------- Previous update was at 03:58 PM ----------

When the first word contains slashes, the shell attempts to execute the executable at that location; when the first word does not contain slashes, the shell goes through a lookup process that checks for the existence of functions and builtins before it tries a $PATH lookup. In either case, aliases are expanded beforehand.

Regards,
Alister

GNU bash, version 4.1.5(1)-release (i686-pc-linux-gnu)

---------- Post updated at 08:09 PM ---------- Previous update was at 07:53 PM ----------

Regarding your problem, perhaps your environment has aliased echo or has defined a function that overrides the builtin. In bash, the following command would indicate exactly what your shell is trying to execute (whether you type it at the command line or in a script):

type echo

Regards,
Alister

Both return echo is a shell builtin

Plus,when I type the command

echo --version

in bash it return

--version

when I type

echo --help

int bash it return

--help

But when I type /bin/echo instead of echo,everything goes right.

Anything difference between the builtin echo and the /bin/echo?

Perhaps you have posix mode and xpg_echo set?

What output do you get from

 $ shopt xpg_echo

Try setting xpg_echo mode off:

$ shopt -u xpg_echo

The first command return xpg_echo off

Thats a shame I could duplicate your issue by setting posix mode and xpg_echo:

$ set -o posix
$ shopt -s xpg_echo
$ echo -e testing
-e testing

Or

$ bash --posix -O xpg_echo
$ echo -e testing
-e testing

Perhaps your shell was compiled with --enable-strict-posix-default

I got the same output when I type the code above.
-e testing

And if my shell is using posix by default,when I type

echo -e

it should print -e ,right?
But it just print a blank line.

By the way,when I want to turn off the posix using set command,what should I type?

set +0 posix

to turn off

So is it correct that echo -e gives a blank line; but echo -e "Hello,World\a\n" gives:
-e Hello,World

echo -e "Hello,World\a\n" goes right when I type it in my bash,but goes wrong when I add it in my shell script.

Ah does the script have "#!/bin/sh" at the top, cause bash will go into posix mode if it's called with the name sh

I use #!/bin/bash in my script.

Plus,

bash firstscript.sh

output
Hello,World

sh firstscript.sh

output
-e Hello,World