This question is concerned with the negative impacts of using external programs versus instead of built-in constructs -- specifically concerning sed
, and external programs in general.
My thinking is that in order to maximize compatibility across UNIX systems, one should use builtin commands. However, some programs are virtually standard. Consider this example:
# Both functions print an array definition for use in
# assignments, for loops, etc.
uses_external() {
declare -p $1 \
| sed -e "s/declare \-a [^=]*=\'\(.*\)\'\$/\1/" \
| sed "s/\[[0-9]*\]\=//g"
}
uses_builtin() {
local r=$( declare -p $1 )
r=${r#declare\ -a\ *=}
echo ${r//\[[0-9]\]=}
}
In terms of compatibility, is there much of a difference between uses_builtin()
and uses_external()
?
With regards to compatibility, is there a certain class of external programs that are nearly universal? Is there a resource that gives this kind of info? (For the example above, I had to read though many sources before I felt comfortable assuming that sed
is a more compatible choice than awk
or a second language.)
I really want to weigh the pros and cons, so feel free to point out other considerations between builtin commands and external programs (i.e. performance, robustness, support, etc). Or, does the question of "builtin vs external" generally a per-program matter?
Objectively speaking, using built-in commands is more efficient, since you don't need to fork any new processes for them. (Subjectively speaking, the overhead of such forking may be negligible.) Large numbers of built-in commands that could be subsumed by a single call to an external program may be slower.
Use of built-ins may or may not produce more readable code. It depends on who is reading the code.