3 ms·
> You can't even return a value from a Bash function (just a numeric status code). You can print a textual result and store it in a variable, which is a pretty
by geofft 9y ago
> You can't even return a value from a Bash function (just a numeric status code).
You can print a textual result and store it in a variable, which is a pretty natural pattern in bash, e.g.,
set -e
download_command () {
if type wget >/dev/null 2>&1; then
echo "wget -q -O-"
elif type curl >/dev/null 2>&1; then
echo "curl -sL"
else
echo "Error: curl or wget is required" >&2
exit 1
fi
}
download=$(download_command)
public_v4=$($download http://whatismyip.akamai.com/)
public_v6=$($download http://ipv6.whatismyip.akamai.com/)
The numeric status code is best used to indicate errors (as above, with set -e), not to overload for data.
I do agree that I'd prefer being able to return actual structured data and not strings -- but honestly you can pass e.g. JSON back-and-forth between commands that take JSON, and into `python -c 'import sys, json; something(json.loads(sys.stdin))'` if you need it for a small part of a script that's generally better written in sh.
- cardiffspaceman 9y agoI roughed out a LISP interpreter in Bash. Since I just stored s-expressions in bash variables and/or returned s-expressions with echo and/or parsed the parens to compute cdr or car, it was not a big success.