0

When I run this script on my terminal it works perfectly with the newline

echo -e $(echo "a \na")

Outputs:

a 
a

When I wrap this up in a bash script - test.sh:

#!/bin/sh

echo -e $(echo "a \na")

And I call ./test.sh, I get this output:

-e a a

How can I make the bash script give the same output with the newline as running directly on the terminal?

ClickThisNick
  • 5,110
  • 9
  • 44
  • 69

1 Answers1

2

Use printf. The echo isn't portable. Some shells doesn't knows the -e and like. Also, using printf you will get more formatting options.

printf "%s\n%s\n" a a
#or
printf "a\na\n"

The answer to your question is in the @that other guy's comment:

Make the shebang #!/bin/bash, otherwise it's a sh script and not a bash script

e.g.

sh test.sh
# -e a a
bash test.sh
# a
# a
dash test.sh
# -e a a
zsh test.sh
# a a

so... use printf - will work everywhere and give the same result.

Community
  • 1
  • 1
clt60
  • 62,119
  • 17
  • 107
  • 194