I’ve noticed echo behaves slightly differently when called directly
root$echo "line1\nline2"
and when called via a script:
#! /bin/sh
echo "line1\nline2"
[...]
The first case would print:
line1\nline2
, while the latter would print
line1
line2
So echo is always assumed to have flag -e when used in a script?
Your script has a shebang line of
#! /bin/sh, while your interactive shell is probably Bash. If you want Bash behavior, change the script to#! /bin/bashinstead.The two programs
shandbashare different shells. Even on systems where/bin/shand/bin/bashare the same program, it behaves differently depending on which command you use to invoke it. (Thoughechodoesn’t act like it has-eturned on by default in Bash’sshmode, so you’re probably on a system whoseshis reallydash.)If you type
shat your prompt, you’ll find thatechobehaves the same way as in your script. Butshis not a very friendly shell for interactive use.If you’re trying to write maximally portable shell scripts, you should stick to vanilla
shsyntax, but if you aren’t writing for distribution far and wide, you can use Bash scripts – just make sure to tell the system that they are, in fact, Bash scripts, by puttingbashin the shebang line.The
echocommand is probably the most notoriously inconsistent command across shells, by the way. So if you are going for portability, you’re better off avoiding it entirely for anything other than straight up, no-options, no-escapes, always-a-newline output. One pretty portable option is printf.