4 ms·
The problem with Bash functions is that they aren't really functions -- more like subroutines. You can't even return a value from a Bash function (just a numeri
by l_t 9y ago
The problem with Bash functions is that they aren't really functions -- more like subroutines. You can't even return a value from a Bash function (just a numeric status code).
Most of the functions the author wrote wouldn't even be possible in Bash, if only because they utilize return values. So your suggestion that he "compare similar programming styles" is practically impossible without using implicit state-passing methods in Bash, which are much more awkward and error-prone than real functions.
Having said that, I agree that writing scripts in Python adds a lot of overhead over Bash, and should only be done if you are implementing significant complicated logic more than just I/O. (For example, retries, exponential backoff, business logic, exception handling, etc.)
- mbrock 9y agoYou can output data from a function and pipe it just like any other program, right? So that's how you tend to return values from bash functions.
- 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.
- e12e 9y agoReturn-values in (ba)sh is text output. You can pipe to and from a function in bash, and you can use replacement [eg: host=$(hostname)]. It's different, but it's a powerful paradigm.
- seorphates 9y agoI don't understand your first statement. I thought that if you wrote your own function/routine/blah you can have it return anything you might like to handle - in one side and out the other. {print_this_garbage_out=`do_a_little_dance` #maybe even with awk|sed|perl|python|sql|mom retval_for_something_hopefully_cool=$? # or some other requisite test/set, limit sky echo "${print_this_garbage_out}" return ${retval_for_something_hopefully_cool}} # don't exit, because that might be silly The bash is sort of like your machine code, it, like your machine, can use anything it has access to, if told properly. So I don't understand "can't even return a value". I don't know much about bash for bash's sake so I'm wondering if I may have missed something along the way.