POSIX itself contains some interesting notes about `echo` and `printf`, e.g. from the `printf` page:
"The printf utility was added to provide functionality that has historically been provided by echo. However, due to irreconcilable differences in the various versions of echo extant, the version has few special features, leaving those to this new printf utility, which is based on one in the Ninth Edition system."
The behaviour of `echo` is especially inconsistent for escape sequences. Often, `echo -e` is used to enable escape sequences for it but this is not POSIX compliant.
For any "standards-perferring" shell scripting pages I'd thus think `printf` is the right way to present it if only to advertise that this builtin exists.
> should likely be done with external tools just because people understand "oh, awk or sed -- I know this!" instead of "what the hell is this line noise?"
I am not actually sure about this "I know this" part. I think many users of AWK only ever use it to do some sort of "access the n-th column of input" as in `echo a b c | awk '{ print $2 }'`. Similar for `sed` which is often only known and used for "replace string a by string b in input" use cases.
> If you're writing performance optimized shell, well, probably put the keyboard down
There is some difference between performance optimized and "hugely inefficient" that can make the difference between a task taking minutes vs. a few seconds. It may not seem much but shell scripts are often integrated into automated processes (think of build environments or system startup tasks) and there, seconds quickly accumulate. Whether this is relevant to your use case, only you can know.
I found this "Pure Sh Bible" to be highly interesting, but there are some places where it has some rough edges and it may not show the "best practices" for standard use cases. Still a very useful resource.
Sed has at least the vi users who may know ex command from the vi command mode. Awk needs to be learned for itself.
Don't play the game. Don't do things where it matters what the exact output of echo gives you.
Did you know that you can't put a null into a shell variable? You can't. Also, you shouldn't ever be in a position to care. Did you know that you can fiddle with the "IFS" to let you parse CSVs? Don't ever do that; it works but nobody will ever understand what the hell you did. You can make an case statement and evaluate it with eval to make your own shell script that writes its own shell scripts. Please don't.
All of these adventures are possible, and work fine, and I've done them and come back to the code a decade later and I both understand what I was trying to do and the code still works across a variety of bourne shell interpreters. Nevertheless, these are things that shouldn't be done except to flex.
N="$(printf '\033[')" x=30
for a in Bl R G Y B M C W # 4-bit Black Red Green Yellow Blue Magenta Cyan White
do eval $a='$N'"'"$(( x))"m'" \
b$a='$N'"'"$((60+x))"m'" \
${a}bg='$N'"'"$((10+x))"m'" \
b${a}bg='$N'"'"$((70+x))"m'" # bX=bright Xbg=background bXbg=brgt bgnd
x=$((x+1))
done
N="${N}0m"
printf "$Y${Gbg}I am yellow on green.$N\n"
I pasted this into my Android terminal, and it works as advertised.on green is all that prints out? What's the rest of it for?
That's only an example. Use them all, if you want.
Also, don't use echo, use printf. echo has different semantics and potential footguns depending on the platform.
Here's a common pattern for appending to PATH system-wide once:
# /etc/profile.d/foo.sh
DIR=/opt/foo/bin
case ":$PATH:" in
*":$DIR:"*) ;;
*) PATH="$PATH:$DIR"; export PATH ;;
esac
unset DIR
If zsh or bash is available: # $1 var
# $2 prepend this string to var
# $3 separator (: default if omitted)
# doesn't remove or prevent duplicates
prepend() {
eval "export $1=$2\${$1:+${3-:}\$$1}"
}
# $1 var
# $2 append this string to var
# $3 separator (: default if omitted)
# doesn't remove or prevent duplicates
append() {
eval "export $1=\${$1:+\$$1${3-:}}$2"
}I'd personally try to minimise the use of eval, e.g.
prepend() {
local val
eval "val=\$$1"
export "$1=$2${val:+${3-:}$val}"
}
append() {
local val
eval "val=\$$1"
export "$1=${val:+$val${3-:}}$2"
} var=world
declare -n tmp="hello_$var"
tmp=value
printf '%s\n' "$hello_world" # value
I highly recommend this pattern if you want to emulate jagged 2d arrays or other nested data structures in bash for some reason.
For instance, using eval to have variables with variable names is madness.
$ var="world"
$ eval "hello_$var=value"
$ eval printf '%s\n' "\$hello_$var"
I suppose the fancy business with printf is flexing, but I suspect the majority of people scanning documents like this would prefer to have examples with echo "${variable%%string_modifier}"
Escape sequences are mostly dependent on you being on a VT102 compatible terminal; I'm not sure what happens if you're on an ASR33 and you send this to it...
I'd consider type checking (is this a float) to be similarly cursed witchcraft -- after all I've run into all sorts of "floats" that aren't just 123.456 strings...
Overall -- there's a huge range of things shell can do that you probably should just avoid. Similarly, even if you can do it in a clever shell way (the bit shifting math for instance, or extensive string manipulation) should likely be done with external tools just because people understand "oh, awk or sed -- I know this!" instead of "what the hell is this line noise?" If you're writing performance optimized shell, well, probably put the keyboard down and walk away for a bit and reconsider your life choices.
Good doc; I'll probably stick to the man page though.