4 ms·
My bash scripting cheatsheet after maintaining a couple tools for 4+ years: use system Ruby / Python or anything else instead, really. Once over a hundred lines
by ricardobeat 8y ago
My bash scripting cheatsheet after maintaining a couple tools for 4+ years: use system Ruby / Python or anything else instead, really. Once over a hundred lines of code it becomes a massive pain to work with.
- deleted 8y ago[deleted]
- juddlyon 8y agoPython is fantastic if it requires functions or anything over several lines. I'm not a Python dev but I can very quickly find standard library examples or well-documented packages. Perl/Ruby are too clever for me.
- tracker1 8y agoI've been doing similar with node+shelljs for similar reasons. Especially with Windows being used by a lot of devs where I work. This cheatsheet is still a nice to have though.
- RcouF1uZ4gsC 8y agoFor Python, I have used plumbum https://plumbum.readthedocs.io/en/latest/ https://plumbum.readthedocs.io/en/latest/ which basically makes Python as terse as bash for common use cases and greatly simplifies the complicated stuff.
- h1d 8y agoShell scripting has a place and script languages aren't always the answer for improvement. For many tasks it can even be more verbose but logic crafting can be easier. You should rather look for languages like fish, which makes shell scripting feel better than drop it altogether.
- oxguy3 8y agoThe biggest benefit to Bash scripting over a friendlier language is that Bash is guaranteed to just work on pretty much every machine. Bash is on virtually every *nix machine (except maybe embedded devices), and it's extremely resilient to time. Looking through the change history (https://wiki.bash-hackers.org/scripting/bashchanges https://wiki.bash-hackers.org/scripting/bashchanges), I'd wager that 99% of scripts don't use anything that wasn't in Bash 2.05b, which came out in Jul 2002. Anecdotally, the "newest" feature that I've ever used is regex support in conditional expressions, which was added in 3.0-alpha (Aug 2004). Ruby and Python, meanwhile, are not quite so universally installed as Bash, and they have had all sorts of major breaking changes since 2004. And even if they are installed and version-compatible, they still might not work. Countless times have I seen a misconfiguration with Python 2 vs 3 or RVM or the PATH variable or whatever else cause errors, but I have never run into a scenario where a Bash script didn't just work. All that said, I'm totally with you that Bash scripts should be short and sweet. If you're doing something with over 100 lines, Bash's downsides overtake its benefits.
- roryrjb 8y agoIt's true that bash can be installed on any Unix-like OS, but so can Python, Perl, etc, but it's not available by default, on OpenBSD there's pdksh, FreeBSD tcsh iirc, and on MacOS I believe the version of bash is really old due to some previous license change. Edit: I read your post quickly, you addressed the old bash scenario, but still in my opinion I would always, and do target POSIX shell for maximum cross-platform compatibility and that is definitely available on Unix-like OSs by default.
- ricardobeat 8y agoThat’s true, and it’s the reason the tools I mentioned were written in Bash in the first place. Python 2/3 was a pain, Ruby had very disparate versions available. But now, a few years later, you can assume at least Ruby 1.9 nearly everywhere, and OTOH still can’t use any of the Bash 4 features. There also quite decent cross-platform packaging solutions for python, ruby, node, go, crystal, mruby that make it really hard to choose bash for any serious project.
- oweiler 8y agoWhile Bash itself is portable, the commands Bash scripts glue together are often not.