436 points · by todsacerdoti · 5y ago · 285 comments
If you use Bash for programming, you have to stop thinking in terms of the holier-than-thou software engineer, whose ego believes that a superior, "clean" design makes a superior program. You should embrace globals. You should switch between using or not using the enforcement of set variables or program exit status. You should stop using Bashisms and subtle, obscure language features unless you absolutely have to.
Bash is not a "real" programming language, so do not treat it as one. Do not look for hidden features, or try to do things in cute ways that nobody else uses. There is no superior method or hidden knowledge. Just write extremely simple code and understand the quirks of the shell.
Also, JavaScript really is not a very good programming language. We are just stuck with it because it's the only language the browser understands. (Well, until recently: things are going to change with the introduction of wasm).
But for the shell, we're not stuck with any one language. Whatever you want to do can be programmed in your favorite language. You can easily write a python script instead of a bash function.
people have been saying this for years already and nothing seems to have come of it :(
> JavaScript really is not a very good programming language.
Since 2015, JavaScript really is a nice language. I never thought that I'd say that.Go look at ES6 (the javascript version that came out in 2015) and even TypeScript. You'll be pleasantly surprised, no matter if you're coming from C++, Java, C# or even other "scripting" languages such as PHP and Python.
A couple of years ago, I challenged myself to write some complex scripts and apps using only Bash, and came to this conclusion myself.
You can get pretty far with just Bash alone, especially if you strive to write readable and maintainable code, and not just one off scripts. If you need access to data structures, especially nested ones, you can shell out to another language and then print the results back to stdout so Bash can use them:
$ get_data | transform_data | python3 ./complex_script.py | consume_resultsshould read
"no reason to think ... couldn't"
You can use I/O redirection with arbitrary file descriptor numbers, and you can let the shell pick them. So you can `mknod` a pipe, `exec {pipefd_rw}<>"$pipe_name" {pipefd_ro}<"$pipe_name" {pipefd_rw}>"$pipe_name";` and now you have three open file descriptors that you can let the sub-shell use to communicate with the parent. And so you're not limited to stdin/stdout. I do wish there was a built-in for making an anonymous pipe instead of having to make one on filesystem.
(You have to open the named pipe for read-write first because otherwise you'll block, but if you don't want a read-write FD for it you can close it after opening the read and write FDs.)
I'll count myself among nobodies, I guess. I use them a fair bit when part of said subshell cd's somewhere, and I don't feel like using pushd/popd, either of which might fail to operate based on my mistakes, but subshells seem to never fail to exit, eventually.
The thing with bash is, it's great for tying things together and quick bits and pieces, but it's not set up for writing maintainable code. Arrays, functions, even if statements comparisons can all be done in bash (as first-class features), but are just... easier in other languages. And then think about the refactoring, linting, testing tools available in bash vs other languages. And then on top of that, there's the issue of handling non-zero return codes from programs you call; do you `set -e`, and exit on any non-zero return code even if you wanted to continue, or not `set -e`, ignoring any errors as your script just continues.
Personally, when I feel I want to use a function (or array, or other similar, non-trivial thing), in bash, it's time to reach for another language.
Having said that, there are some nice programs written in bash. https://www.passwordstore.org/ being one that comes to mind.
Python is strongly typed. Maybe you meant "statically"? (As opposed to dynamically.)
Then you must not be writing any bash at all. Functions are useful for almost anything beyond a one-liner script.
Typical functions used in innumerable non-trivial scripts:
* print_usage()
* die() (see: https://stackoverflow.com/q/7868818/1593077)
:-)
https://github.com/GavinRay97/hasura-ci-cd-action/blob/a9731...
This is out of necessity. I'm not the sharpest tool in the shed, so I have to go out of my way to write things such that when I come back to them in months or years, I still understand what they do.
For this same reason, is also why I never use shorthand flags for scripts.
I have no clue what IE: "-s -y -o" might do (or even worse, "-syo", dear god my eyes! Also -- is that one special command, or a series of individual commands?!).
But "--silent --assume-yes --output-file" is pretty easy to grok immediately.
https://ilya-sher.org/2021/03/19/running-elegant-bash-on-sim...
Parsing command-line arguments is easy, but parsing them correctly is ridiculously hard. `getopts` doesn't handle long flags, so it's out. `getopt` doesn't handle arguments with spaces in them, unless you have a version with non-standard extensions, so it's out. You're left with manual parsing, and it's a royal pain to make sure you handle every expected case (short/long flags, clusters of short flags, trailing arguments to be passed through to a subprocess, short/long options whose value is in the next argument, long options whose value is after an equals sign, and several others I'm probably forgetting about). And this is just to get the arguments into your script, before you actually do anything with it.
I agree that bash has effective idioms, and learning those idioms can make scripts easier to write. I strongly disagree that bash is "as maintainable as anything else", and scripts beyond a few hundred lines should be rewritten before they can continue growing.
while getopts :i:h-: option
do case $option in
# accept -i $input-file, -i$input-file
i ) input_file=$OPTARG;;
h ) print_help;;
- ) case $OPTARG in
help ) print_help;;
help=* ) echo "Option has unexpected argument: ${OPTARG%%=*}" >&2; exit 1;;
in=* ) input_file=${OPTARG##*=};;
in ) echo "Option missing argument: $OPTARG" >&2; exit 1;;
* ) echo "Bad option $OPTARG" >&2; exit 1;;
esac;;
'?' ) echo "Unknown option $OPTARG" >&2; exit 1;;
: ) echo "Option missing argument: $OPTARG" >&2; exit 1;;
* ) echo "Bad state in getopts" >&2; exit 1;;
esac
done
shift $((OPTIND-1))Is there a reason we aren’t using a shell with a proper programming language for scripting?
I can’t say I’m a fan of bashscript. I wonder why we are using this weird language.
After all, HTML got much better when WHATWG took W3C's toys away and actually started innovating HTML again. Maybe it's time to do something similar with Unix: I'm sick and tired of people telling everyone that we have to stay in some 1995 time capsule because we need to follow The Standard and if The Standard hasn't changed, too bad.
De-facto standardization has at least brought us free officially-supported options for running Bash or other *nix shells on both of those platforms(if terribly outdated on Mac).
Maybe it'll be like the C++ vs Rust situation?
$ cat "file.txt"
...and I don't even know what options would look like... maybe: $ ls "*.txt", ["l", "a"]
I dunno... this would mean we'd probably need parens now for precedent-type concerns: $ ls("*.txt", ["l", "a"]) > "file.txt"
Anyway, that is why I understood that it's this way. I can't point to a resource about it, though.With bash (and I suspect with every other popular shells out there), it actually means something different. In your second example, if you don't use quotes, the shell will do the extensions, but if you do, the process will have to do it (and in the case of ls, it cannot).
In some case it's good to have the shell taking care of it, but sometimes (eg, when using 'find') it's not.
Here are the ways to run a program in ruby https://stackoverflow.com/questions/7212573/when-to-use-each...
https://research.swtch.com/shmacro
The Bourne shell thus adopted ALGOL syntax, distinct from anything else in UNIX.
David Korn brought a pile of C, and wedged it into a max 64k program space (for Xenix), and his upward-compatible Korn shell (ksh88) had many, many features.
Then there were the "UNIX Wars"...
https://en.wikipedia.org/wiki/Unix_wars
The fallout of this was the choice of a few Korn features, but not all (no arrays, coprocesses, [some] eval, regex, et al.)
The wars defined the POSIX shell. Yes, it can be infuriating.
There are two very distinct places where Bash is not, that I have found.
Bash is not in busybox. It pretends to be, but what is really there is the Almquist shell, with a bit of syntactic sugar to silence the common complaints.
Bash is also not in Debian's shell (/bin/sh). In that place, there is no tolerance of bashisms.
It is important to know the POSIX shell for these reasons.
You have to step back and remember the point of all this. Why are you programming? To solve a problem, and make a human's life easier. What is the problem you are trying to solve? How can we make the human's life easier? In this case, it's "I want to combine bunch of programs together in a command-line interface, to make it easier to use these programs to get my work done on the command-line." So, what solution should we choose for this scenario?
Lots of different "scripting" languages exist (we used to call them "glue languages"). Today Python is the most popular, and before it was PHP and Perl. But who cares if they're popular? What are they good for? Why would you choose one over the other?
Python is a general purpose language which is easy to learn, easy to write, and easy to read. Well that sounds nice, but it's very "general" and not specific to our use case. PHP is a language designed for use in web development. Perl is a language designed for system administration-type tasks.
Bash scripting is designed for making it easy to combine already-existing programs in a command-line shell, with no modification or creation of programs required. The only "dependency" is the Bash interpreter (which happens to also be the command-line interface we started our problem with! how convenient!) and an existing collection of software tools that work with each other through a command-line interface. Every part of it is explicitly not designed to "make programming easier". Instead, it is designed to make "combining programs in the command-line" easier. The result is things like every single word you type is potentially just an outside program being executed, or arguments to that program. No other language has this quirky behavior, because they're not designed solely to make command-lines easier to use.
I know Perl well, and the language is fantastically useful as a system scripting language. It combines some of the best features of common shell scripting programs with more programming language-specific features. It is designed with shortcuts and "magic" to make quick work of command-line scripting tasks. But ultimately Perl was not built into the command-line, so its utility is always a bit stilted. Going between the shell and the Perl code and back to the shell isn't as flexible as if the Perl code was embedded in the command-line. Perl also doesn't have as simple a facility for pipes as a command-line shell does. Oh, and a Perl install is rather large, isn't available everywhere by default (anymore), and has all the usual dependency problems. So even though I know Perl very well, if I need to just combine existing programs in the command-line, I always use Shell scripting instead of Perl. (There are Perl shells which solve this problem, but then everyone needs to learn Perl, and Perl can be.... idiosyncratic)
You could also use Awk for a large number of general programming tasks, but again it's designed for a different specific problem: pattern-scanning and processing, not generally combining programs together.
So we use shell scripting to solve the problem because it was designed specifically for our problem, it's ubiquitous, and simple to use. You could use any other programming language, but it wouldn't fit our scenario as well. Once your use case changes, and you are no longer only trying to combine existing programs together, or the existing programs don't do what you want them to do, then you need to use a different language that better fits that new use case.
Mostly because the people who want to introduce a "programming language" into the shell don't prioritize being a shell.
Check out the "Oh" shell for contrast. This is what a programming language looks like when you force it to conform to being a shell first priority.
https://github.com/michaelmacinnis/oh
https://www.youtube.com/watch?v=v1m-WEZz46U
This is "Scheme-like" (actually the late John Shutt's Kernel-like) but has FEXPRs so things can be redefined and evaluation can be controlled.
However if I’m not careful they get painfully complex and I tend to regret not writing it in perk. As such I now switch to using Perl when I get to the function stage, or anything other than the most simple bash arithmetic.
> Getting anything out of a subshell that isn't from STDOUT is impossible.
Yes that is a known limitation that has horrible, horrible workarounds. Don't use them.That said, for scripts whose main input _is_ from a file or STDIN, and also whose main output _is_ to a file or STDOUT, then bash is far more often the right tool for the job than it is given credit for. Of course, I'm talking about bash and a host of other utilities that are often packaged with it such as awk, grep, sed, cut, etc.
For processing text I find myself often choosing between bash and Python, and very often if I choose bash I'll feel that I've made the right choice.
djb has always used them, even in the shortest of scripts.
Otherwise I agree with everything in this comment.
There is a reasonable argument to avoid using bash for non-interactive scripts. The benefits of any additional bash features, so-called "bashisms", arguably do not outweigh the costs of making these scripts non-portable. One of the many advantages of shell scripts is that they are portable and have tremendous longevity; shell scripts can last a long, long time. There are no version changes and aggressive feature creep to worry about as is routinely the case with programming languages. The scripts just keep working, every day, and we can forget we are even using them, e.g., they are being used in the various ways by UNIX-like OS distributions.
One of the today's programmer memes was "Get Stuff Done". Maybe the shell was not meant for today's programmers. But for "sysadmins", or "DevOps", or whatever term anyone comes up with in the future, people who can "administer/operate" computers running a UNIX-like OS for themselves or for someone else like a client or an employer, the Bourne shell works for its intended purpose, better than anything anyone has come up with in the last 50+ years. There's a lot of stuff that "gets done" with the shell, whether it is on someone's own computer, their client's or their employer's.
Attempts at "shell replacements", cf. alternative shells, usually look like interpreters for programming languages, not shells. Perhaps this is not a coincidence.
Bourne shell is relatively small and fast. Computers with small form factors often use UNIX-like OS and when they do they usually include shells. Many programmers seem to dislike the notion that such a layer exists. They often try to blame the shell instead of their own lack of interest in learning to use it.
The shell is boring, and "boring" is sometimes the wisest choice. Most software has expanded to consume available resources (often the developer's choice not the user's). This makes computer performance gains difficult for the end user to discern, e.g., decade after decade, routine tasks seem to take the same amount of time. However the shell and many "standard" UNIX utilities have not changed as much in the same period. Subshells may have been "slow" many years ago. IMHO, they do not feel slow today. Routine tasks performed with the shell seem to run faster, as one would expect after hardware upgrade.
I posit: Life is too short to learn every programming language du jour but it is long enough to learn the Bourne shell, reasonably well.
If scripts written for a POSIX-like "lowest common denominator" shell were not portable, methinks projects written for such a shell, such as autoconf, would not work on so many diffferent systems. The amount of free software found in open source OS repositories that is built using "configure" shell scripts is not small.
Debian and other Linux distributions use a shell derived from NetBSD's Almquist shell. People who design and maintain these operating systems suggest that, for non-interactive use, this Bourne shell is faster than Bash.
They have to shell out to other commands in order to do something meaningful.
Depending on your OS, these commands may not exist, or have a different set of commandline flags.
Mind, if possible, it is even uglier than using stdout.
err_f="$(mktemp)"
third_f="$(mktemp)"
out="$(cmd 2>"$err_f" 3>"$third_f")"
err="$(cat "$err_f")"
third="$(cat "$third_f")"
Seems like that should work fine, and it's not even that odd looking or hard to understand. But I haven't tried, not sure if there's some surprise lurking in the depths of tempfiles or something.The fact that there isn't a good way to open an anonymous pipe and get its two file descriptors in bash is very annoying.
> avoid bashisms
What's holier-than-thou is insisting that people stick to a dialect of shell scripting that hasn't changed in decades because some people think that conforming to POSIX is its own reward even if it makes life harder and programs worse.
No, thanks. I'll stick to bashisms. Things like coprocesses and filemap make certain classes of program much easier to write. And yes, shell scripts are programs, and the quickest way to turn these programs into unmaintainable balls of mud is to follow your advice to avoid normal programming best practices simply because the program you're writing happens to be a shell script.
#!/usr/bin/env bash
process_line() {
if [[ $* == *foo* ]]; then
echo "found_foo=1" >>$IPC
fi
}
IPC=$(mktemp)
find . -maxdepth 1 -type f | while read line; do
# note: this is run a subprocess
process_line "$line"
done
source "$IPC"
echo "found_foo=$found_foo"Bash is definitely a different paradigm to your average imperative or functional language and thus requires a different approach but I wouldn’t go so far as to say it’s not “real”.
It certainly fits the criteria of a programming language even if it does have more warts than a pantomime witch.
Compared to other languages, Bash can be incredibly worse for some things, but much better for others.
People who don't realise that won't be able to get the best out of their toolset.
If anything you are saying it should be deprecated in place for a real language. If people want to do more advanced things why does the basic language of their shell not support it? Why should you not care about the correctness of your shell scripts which can run important things? Seems a perfect place for a functional language. It’s high level, you can just declare what you want. I imagine your interpreter can add in all sorts of checks to help make your scripting more correct.
And it’s a horrible shell experience.
It’s great for scripting (but not as good as other scripting languages) but it’s a terrible experience on a day to day basis, with the only salvation being the existence of 2-3 letter aliases which make things slightly more manageable.
Bash is a great shell language that whose scripts are useful because it allows you to trivially convert your manual shell actions into an executable script.
If you need to do anything complex (or rather, something you probably wouldn’t type into the shell manually if you had to do the thing on a one off basis), then you’re probably better off using a different scripting language.
But changing BASH with scripting as a priority would probably make it a worse shell language (which is not to say there are no improvements to be made…there are).
Huh. How does one return a value from a bash function? I've always only ever seen the "echo calling convention" used in scripts, or use of global variables.
Since all valid "sh" syntax is valid "bash", you've got to restrict yourself to only sh-valid code in the event the host doesn't have bash.
I have gotten used to doing this -- these scripts usually wind up being run in Docker containers that don't have "bash" installed for the sake of minimalism.
IE, you can't use [[ $predicate ]], but instead [ $predicate ], etc. Lot of subtle differences.
The supersets of POSIX are often even more effective.
Ugly as the tool may be, in the box it stays.
Edit: It seems I should elaborate.
> Getting anything out of a subshell that isn't from STDOUT is impossible. So you can't define an array in a subshell and then use it outside the subshell, and you can't return an array (or anything that isn't a string) from a subshell. If you only use subshells and want to use any kind of data structure that isn't a string passed from STDOUT, you have to do it globally.
Yes, but that is no problem at all, because that is the way shell scripts work and if you do it in a functional way, where the function has no state, it is great (we a few exceptions).
> And subshells are slow. So nobody uses subshells.
Shell scripts are slow in general, subshells don't make an exception but also don't have a large impact. And people do use subshells.
> If you use Bash for programming, you have to stop thinking in terms of the holier-than-thou software engineer, whose ego believes that a superior, "clean" design makes a superior program. You should embrace globals. You should switch between using or not using the enforcement of set variables or program exit status. You should stop using Bashisms and subtle, obscure language features unless you absolutely have to.
If you use Shell scripts, you should understand that this language has been designed decades ago and that professionals advise to use it just in short scripts to connect binaries.
> Bash is not a "real" programming language, so do not treat it as one. Do not look for hidden features, or try to do things in cute ways that nobody else uses. There is no superior method or hidden knowledge. Just write extremely simple code and understand the quirks of the shell.
That part is mostly okay, but "real" doesn't get the point, as it is a real language, just one with many problems. However, given its superior strength we haven't overcome it yet...
"Superior strength" is probably just a matter taste, however I would really like to read your explanation on why you think so.
Some reasons why POSIX shells are great:
- they are part of POSIX (a Standard which is implemented by multiple OS) and therefore often available
- you can use them to compose programs written in different languages
- you can easily connect the output of one program with the input of another (pipes)
It doesn't trigger EXIT, but it does trigger RETURN. Just trap both:
#!/bin/bash
foo() {
trap "echo 'Cleanup!'" RETURN EXIT
#return
#exit
echo "Kill me with ^C or \"kill $$\""
while true ; do : ; done
}
foo # should print 'Cleanup!' on SIGTERM,
# returning, or calling exite.g.: wtfp.sh
#!/usr/bin/env bash
map() {
local fn="$1"
local input
while read -r input; do
"${fn}" "${input}"
done
}
reduce() {
local fn="$1"
local init="$2"
local input
local output="${init}"
while read -r input; do
output="$("${fn}" "${output}" "${input}")"
done
echo "${output}"
}
filter() {
local fn="$1"
local input
while read -r input; do
"${fn}" "${input}" && echo "${input}"
done
}
add() {
echo $(( $1 + $2 ))
}
increment() {
echo $(( $1 + 1 ))
}
square() {
echo $(( $1 * $1 ))
}
even() {
return $(( $1 % 2 ))
}
sum() {
reduce add 0
}
map increment | map square | filter even | sum
...then: $ printf "%s\n" 1 2 3 4 5 | ./wtfp.sh
56The issue is that pipe mills are very slow.
$ mill() { while read -r line; do echo $line; done }
$ export FIVE_MEGS=$(( 5 * 1024 ** 2 ))
$ time yes | mill | pv -S -s "$FIVE_MEGS" > /dev/null
5.00MiB 0:00:23 [ 221KiB/s] [============>] 100%
real 0m23.084s
user 0m14.121s
sys 0m26.780s
$ time yes | pv -S -s "$FIVE_MEGS" > /dev/null
5.00MiB 0:00:00 [6.55GiB/s] [============>] 100%
real 0m0.005s
user 0m0.000s
sys 0m0.006s
Even Python loops are faster. $ export PYLOOP="from sys import stdin
for line in stdin:
print(line)"
$ time yes | python3 -c "$PYLOOP" | pv -S -s "$FIVE_MEGS" > /dev/null
5.00MiB 0:00:00 [67.1MiB/s] [============>] 100%
real 0m0.082s
user 0m0.071s
sys 0m0.019s
[1] https://en.wikipedia.org/wiki/Pipeline_(Unix)#Pipemilldash is faster but not as much as I'd expected:
$ time yes | bash mill | pv -S -s "$FIVE_MEGS" > /dev/null
5MiB 0:00:24 [ 208KiB/s] [====================================================>] 100%
real 0m24.614s
user 0m21.338s
sys 0m11.749s
$ time yes | ksh93 mill | pv -S -s "$FIVE_MEGS" > /dev/null
5MiB 0:00:11 [ 447KiB/s] [====================================================>] 100%
real 0m11.451s
user 0m6.480s
sys 0m12.441s
$ time yes | dash mill | pv -S -s "$FIVE_MEGS" > /dev/null
5MiB 0:00:07 [ 663KiB/s] [====================================================>] 100%
real 0m7.720s
user 0m4.687s
sys 0m8.934s
awk is super fast as expected: $ time yes | awk '{ print $0 }' | pv -S -s "$FIVE_MEGS" > /dev/null
5MiB 0:00:00 [11.5MiB/s] [====================================================>] 100%
real 0m0.439s
user 0m0.444s
sys 0m0.011s breaker_breaker() { break; }
foo() { breaker_breaker; }
while true; do
echo Loop
foo
done
bash dynamically crawled up the call stack until it hit a break-able loop. If you squint it almost looks like exception handling! Anyways this no longer works in bash, though it still does in zsh.Dynamic stack-crawling is more or less the most popular historical behaviour (though afaict the Bourne/Korn lineage just silently fails breaks outside the enclosing function); it's just generally consistent with everything else in the shell that's globally/dynamically scoped. Even in the shells where you can't break out of your enclosing function, `eval break` still breaks through loops in your current context, and that kind of looks like a function you called that called break if you squint.
(POSIX expressly leaves this case undefined with a carveout for break/continue inside the body of a function lexically contained within a loop.)
It also reminds me of TCL's "uplevel": https://www.tcl.tk/man/tcl8.4/TclCmd/uplevel.html
(... unless I miss something.)
subshells are ridiculously slow because it's a forked child process. imagine forking for every function call in your program....
> I simply do not understand why people keep recommending the {} syntax at all.
because it's almost always what you actually want.
Not for built-in commands.
> Yeah “fork is slow” is the sort of microbenchmark that is mostly irrelevant for shell scripts
Maybe if you're on a Linux kernel, but not everywhere else.
#!/usr/bin/env bash
set -eo pipefail
function hey {
echo "Hey!"
}
TIMEFORMAT=$'\nTask completed in %3lR'
time "${@}"
Now after running a `chmod +x hey` can run it with: ./run heyFeel free to replace "time" with "eval" too if you don't want your command timed.
This is a really useful pattern because it means you can create a "run" script with a bunch of sub-commands (private or public), auto-render help menus and create project specific scripts without any boilerplate. Bash also supports having function names with ":" in the name so you can namespace your commands like "./run lint:docker" or "./run lint:frontend".
I have a practical example of this sort of thing here: https://github.com/nickjj/docker-flask-example/blob/main/run
I've written about this pattern in more detail here https://nickjanetakis.com/blog/replacing-make-with-a-shell-s.... It's basically a less limited Makefile for when you want to make project specific aliases. This is something I use all the time now.
My shell is my computing environment. It's rude for a script author to make me change my computing environment when he could just as easily have made a script and created his own sandboxed environment that wouldn't interfere with mine.
People should never ship end user interfaces as bundles of shell functions that have to be sourced.
(I'm looking at you, Android build system.)
For example, take the command `./run echo "Don't"`. When using `time`, this will print `Don't`. If you use just `"${@}"`, it will also print `Don't`.
But if you use `eval "$@"`, this will crash with an error like
eval: line 10: unexpected EOF while looking for matching `''If you check the first link in my previous reply it does include an auto-generated help menu that uses compgen to print a list of functions near the bottom. This way you can run `./run` or `./run help` to get a list of commands. I find this is helpful enough without needing completion, especially since every function ends up being a top level command.
On the other hand, with a lot of glue work I do, I eventually want to something more complex (use lists and maps, complex string building and regexes, date handling) and while you /can/ do that in bash, I might as well start in Python and have everything in one language and take advantage of things like code sharing via modules. (And yes you can share code in shell, but again it’s not as nice.)
If there's a modern library/workflow that makes this not the case, I'm all ears!
This is exactly how I felt. Bash can't handle structured data well. Python (being general purpose programming language) can't handle calling external programs well because it's not the "focus" of the language. My shameless plug solution is a "real" programming language that can do both well (along with proper handling of exit codes and more goodies for "devops"y scripting).
https://github.com/ngs-lang/ngs
More about this feeling when you don't have a well fitting language for "devops"y scripting - https://ilya-sher.org/2017/10/10/why-i-have-no-favorite-prog...
Perl and Ruby do both very well. Or at least they do the latter in a simpler way than Python, and they're no worse than it for the former.
I personally use murex but Elvish seems popular too.
1. Tab completion. All major bash-completion packages know how to show Makefile targets in response to a TAB.
2. Parallel, serial, or single-target execution.
3. Automatic dependency resolution between tasks. Tasks that build files can also use timestamps to see what needs rebuilding.
4. Discoverability. Anybody who sees a Makefile will usually understand that something is supposed to be run from the Makefile's directory. Chances are good that they'll check the tab-completions too. There are conventions for standard targets like 'clean' and 'all'.
If you have a project with a build-process that has a bunch of small tasks that you might sometimes want to run piece-by-piece, Make is the perfect tool IMO.
Learning a handful of bash idioms and best-practices has made a massive impact for me, and life much easier. The shell is something you cannot avoid if you're a programmer or other sort of code-wrangler.
You can interact with it + be (mostly) clueless and still get things done, but it's a huge return-on-investment to set up "shellcheck" and lookup "bash'isms", etc.
----
(Off-topic: I am convinced Ruby cannot be beaten for shell-scripting purposes. If I had a wish, it would be that every machine had a tiny Ruby interpreter on it so I could just use Ruby. I'm not even "a Ruby guy", it's just unreasonably good/easy for this sort of thing. And I keep my mind open for better alternatives constantly.)
Example of near-identical script in bash vs Ruby:
https://github.com/GavinRay97/hasura-ci-cd-action/blob/maste...
https://github.com/GavinRay97/hasura-ci-cd-action/blob/maste...
I'm not sure how much closer to describing your exact intent in English a language can get than:
successfully_made_executable = system 'chmod +x /usr/local/bin/hasura'
abort 'Failed making CLI executable' unless successfully_made_executable
Though I have NOT written Perl (either old Perl, or Raku/Perl 6) but I do believe it may be roughly this semantic too.EDIT: Looks like Perl/Raku is essentially the same as Ruby in this regard. So besides it being a whacky language, take that for what you will:
$successfully_made_executable = shell "chmod +x /usr/local/bin/hasura"
die 'Failed making CLI executable' unless $successfully_made_executable.exitcode is 1> successfully_made_executable = system 'chmod +x /usr/local/bin/hasura'
> abort 'Failed making CLI executable' unless successfully_made_executable
I guess, but if I was writing scripts like this, I'd really want to write a helper method something like:
def system_or(cmd, fail_msg)
system cmd || abort fail_msg
end
# and then
system_or 'chmod +x /usr/local/bin/hasura', 'Failed making CLI executable'
Instead of having to write every single `system` call as boilerplate two liners with an immediate-throwaway local var.I wrote this a few years back which ran on bunches of hosts and fed into an infrastructure network mapper based on each hosts' open network sockets to other known hosts. It wasn't really feasible to install a set of tools on random hosts.. but I still had root ssh access across the board. So I needed something tool agnostic, short, auditable, and effectively guaranteed to work:
What does "export" mean in this context? Do you just mean `ssh host cmd` where cmd is some shell that happens to contain (and call) a function?
Try this and you'll see how it returns a dramatic amount of bash as its output:
remote_cmd="`typeset -f` ; function test123 { echo hi! ; }" && echo -e $remote_cmdI would imagine there would be performance implications of defining every bash function as a subshell, is why it's not universally recommended to define functions this way?
No?
#!/bin/bash
fib1() {
if (( $1 < 2 )) ; then
echo $1
return 0
fi
echo $(( $(fib1 $(($1-1))) + $(fib1 $(($1-2))) ))
return 0
}
fib2() (
if (( $1 < 2 )) ; then
echo $1
return 0
fi
echo $(( $(fib2 $(($1-1))) + $(fib2 $(($1-2))) ))
return 0
)
time fib1 15
time fib2 15
Result: 610
real 0m1.585s
user 0m0.590s
sys 0m0.972s
610
real 0m3.168s
user 0m1.068s
sys 0m2.025sCreating a new process for every single function invocation seems crazy to me, and but might actually be just fine for many "ordinary" use cases? (Although might not have been on computers of 20+ years ago, which might also be why it's not something advised, so much of bash tradition is decades old?)
So arguing about the cost of sub-shells is somewhat besides the point.
Actually Oil functions don't use dynamic scope, but this is done in-process, not with another process:
https://www.oilshell.org/release/latest/doc/variables.html#p...
----
Also nested functions don't really add much useful to shell. It's purely a matter of textual code organization and doesn't affect the semantics. I define all functions at the top level.
Are there really cases where subshells are invoked within the same process? In my experience, it has never been the case. That's why I've been trying to minimize the use of subshells because spawning a new process is a bit slow.
ARG_ARCH="amd64"
ARGUMENTS+=(
"ARG_ARCH,a,arch,Target operating system architecture (${ARG_ARCH})"
"log=utile_log,V,verbose,Log messages while processing"
)
utile_log() { echo "$1" }
noop() { return 1 }
log=noop
By creating a comma-delimited list of command-line arguments, the parsing logic and control flow can be influenced from a single location in the code base. The trick is to eval-uate "log=utile_log" when the "-V" command-line argument is provided (or assign ARG_ARCH to the user-supplied value after "-a"). Using the $log variable invokes the function, such as in: preprocess() {
$log "Preprocess"
}
If "-V" isn't supplied, then every invocation of $log simply returns without printing a message. The upshot is a reduction in conditional statements. Function pointers FTW. I wrote about this technique in my Typesetting Markdown series:https://dave.autonoma.ca/blog/2019/05/22/typesetting-markdow...
Here's a rudimentary implementation and example usage to wet your whistle:
https://github.com/DaveJarvis/keenwrite/blob/master/scripts/...
https://github.com/DaveJarvis/keenwrite/blob/master/installe...
run_foo() { echo foo; }
run_bar() { echo bar; }
name=foo
run_$name> "what if that variable isn't set to whatever the developer is expecting it to be set? What random command will then be executed?"
As an author of an alternative shell myself, I purposely removed this ability for that reason.
While I do agree that it would be nice to be able to have 'local' functions and have inter-function cleanup work better, the logical conclusion for me was not to use function subshells. Since the use case is for larger programs (where different functions may want to have their own cleanup mechanisms), I'm opting to go for more of a library route. For example, I'm working on a Bash library that includes a function to allow different sources to add (and remove) functions to the same `TRAP`. A similar function may be useful, possibly involving the `RETURN` trap and the `-T` flag for the use case the author brings up. Obviously, using a package manager for _Bash_ of all languages brings in a lot of overhead, but I think it can be quite powerful, especially with a potential "Bundle" feature that makes scripts work without the package manager.
Concerning specifically the use of subshells, (as other commenters have pointed out) it significantly reduces performance. I also disagree that dynamic scoping is necessarily bad for Bash. I find it quite useful when I need to use various common functions to manipulate a variable - since modifying and 'returning' variables from a function is usually either slow or verbose with Bash. Admittedly though, this feature is quite annoying at times - for example, most public functions in my Bash package manager[2] all have their variables prefixed with two underscores - because they `source` all the shell scripts of all package dependencies - so I want to be extra certain nothing weird happens
[1] https://github.com/hyperupcall/bash-core/blob/a17ab0a8b6070f...
[2] https://github.com/hyperupcall/basalt/blob/main/pkg/lib/publ...
However, I would strongly advice to master a proper programming language. I respect the article and the efforts of the author, but I feel that it is the past.
I mastered Python a bit and the ability to just use things like dictionaries, proper parsing libraries and such, instead of kilometers of fragile pipes, it is so much better.
I understand something like Python may feel total overkill, but that 10 line shell script suddenly needs quite a bit of error handling and some other features and before you know it, you wish you started out with python or something similar.
Besides cleanup, one thing I think I would love to see is a good mechanism for logging. I have started to build a file for functions and then other files, which source that as a library and calls the functions as needed. I would love to be able to tell the library functions to log something if the parent file wants it, print to stderr or stdout by default or be silent if the caller wants that instead.
No, it wasn't smart to do so.
Yes, it still works.
No, I don't know how it works.
{
foo
} always {
cleanup stuff
}`local` is not in POSIX, but is so common in "posix" shells that many use it, just a sidenote.
So maybe it is about bash functions and not posix functions, because it's refencing features that exist in bash but not posix?
https://www.anardil.net/2018/dynamic-shell-scripting.html (self plug)
I recently discovered, similar to the author of the post for this thread, that local variables are dynamically scoped.
I have been writing a lot more shell scripts lately, using a "library" [1] of sorts I've been writing. When I was debugging one of my scripts that uses mycmd, I discovered that I had failed to declare some of my variables local and they were leaking out to the global scope.
I had recently added functionality to call a set of functions on script exit, so I added something that would output the defined variables, in hopes that I could write something that will output them at the beginning and then the end and show the difference. I was surprised when variables defined in my dispatch function [2] for those at exit functions were showing up, even though they were definitely defined as local. It was then that I dug around and discovered the dynamic scope of variables.
I've been trying to figure out how to accomplish what I desire but exclude those variables from calling functions. I haven't been able to find an obvious way to see if the variable is coming from a calling function. I might be able to use techniques like you've pointed out in your linked post to add the tracing that I want. Still need to think more on this.
---
[1] https://github.com/travisbhartwell/mycmd [2] https://github.com/travisbhartwell/mycmd/blob/main/shell/myc...
Is there a way to automate this as a shell script without having to type the password?
I assume
Sudo -s | password | unbind string but that doesn't work.
The other day I just made a sample stripe-jobs cli app with it and playwright.
foo () ( x="hello"; echo $x )
because this creates a subshell. Fun!Or you can just use bash and keep your sanity.
Despite this you can expect your bash scripts written today to work on any of these machines and their ancient OS installs. This is primarily because people writing in Bash care enough to not use new features, not the lack of new features in Bash.
This justification for bash has always been perplexing to me. If I’m operating in an environment where my ONLY reliable infra invariant is “bash will probably work”, cleaning up the org’s infrastructure clusterfuck is probably my #1 priority.
(Or I guess in a few cases the fleet are not boxen but little embedded/iot devices, in which case you probably don't want to be running any of these sorts of scripts for a whole host of reasons...)
> of course you shouldn’t install random gems globally… Or you can just use bash and keep your sanity.
What are some scenarios where you’d need a gem in Ruby but Bash just works?
The only situations I can think of are cases where you’re using Bash to call out to some executable that does fancy things. Which (1) you can do from any scripting language anyways, and (2) means you’re just shifting dependency management from the package manager to the image/Dockerfile.
But actually, the combination of justifications here is particularly perplexing. If the org has a stable way of handling system images, then you'll know which version of $scripting_language should exist and where it's installed. The only way you end up with language version woes is if you don't have standardized infra. BUT... if you don't have standardized infra, and Bash can do things that Ruby can't without special Gems, then it stands to reason that you're in a situation where your Bash scripts depend on the magic state of individual unicorn boxes?! Which is particularly fragile and frightening and far worse than installing some local gems or whatever!
IDK. The purported benefits of Bash always sound like they flow out of environments where there are basically no fleet-wide infrastructure invariants.
"Just use $scripting_language" might be the best advice in this thread just as a sort of canary in the coalmine. I.e., if your org can't "Just use $scripting_language" because "which version?" then the team will probably benefit tremendously in an infinite variety of ways from an afternoon of infrastructure cleanup. Regardless of whether they use bash or a scripting language going forward :)
That is not your job. Your job is to get that machine working using whatever is already installed. Adding a new package means going to the production committee with your proposal and justification and analysis of the increased threat surface.
The defaults are everything.
Or anything that can handle integers and floating point calculations natively.
I think pretty much the only reason people write bash is because you have an incredibly high chance of bash being installed and a version of bash being installed that will run whatever bash you write just fine.
Perl is honestly almost as reliable to be there predictably and compatibly... but I guess people would rather write bash than Perl?
I feel like what usually makes me reach for something beyond Bash is really a matter of wanting or needing some dependency that wasn't written in it for whatever reason. Usually this happens right at the point where the script/utility starts to turn into a library/program, so it's trivial to just transpose the control flow into whatever language is required at that point and go from there. This of course raises the type of concern you mentioned about Ruby, but at that point it's hopefully worth the trouble to address.
And yes, you have these sorts of problems with other languages, but my point here is that Bash doesn't free you from them.
I am not holding my breath.