Live data from Hacker News

Bashing the Bash – Replacing Shell Scripts with Python

medium.com

11–20 of 45 posts

Re: Bashing the Bash – Replacing Shell Scripts with Python

#11
post #2

I think Bash is much more than that. For example, I mostly compose chains of commands through pipes. This extension is not pipe-centric.

I think the scope for Bash has shrunk a lot since Perl and moreso Python have become widely adopted. The rule of thumb I usually have is "if it's more than 50 lines, reevaluate if shell scripting is the best language."

I was working at a place that had been around since the early 80s. It looks like the "official" scripting language was tcsh until around the early 2000s, then Perl was preferred, and around the late 2000s they switched to Python. I ported a bunch of tcsh scripts that missed their Perl transition and was I impressed (I didn't often see "structured" shell scripts with actual design patterns) and the fact they were still used daily. I feel like my Python version had less boilerplate, better error checking, and was more legible.

If you look at the OS itself, a lot of what used to be done with shell scripts migrated to Python2 years ago (which made the move to Python3 tougher)--but the scripts are more legible and maintainable.

I do still love the challenge of running a command and trying to get my result in one line.

Re: Bashing the Bash – Replacing Shell Scripts with Python

#12

I love both bash and python. I almost always write the first version of a script in bash, especially if it involves piping between commands, managing processes, or moving files around the file system. Only when there is significant parsing involved do I reach for python -- and even then, it's often easier to just use python for the bit of parsing that's necessary, within a heredoc using python -c. The example script…

[deleted]

Re: Bashing the Bash – Replacing Shell Scripts with Python

#13
post #7

The point of my shell project Oil ( http://www.oilshell.org/blog/ ) is to get rid of the shell vs. Python debate. My claim is that, in general, X isn’t a good bash replacement, where X is Python/Ruby/Perl/JavaScript. I think that’s obvious to some people but not to others (typically X programmers who don’t know shell, which was me in the not-too-distant past). This article is good because it has a port of a realistic…

There is no [2] in your list of links. Does it refer to to the elevator pitch you included in your post, or did you intend to link somewhere?

Re: Bashing the Bash – Replacing Shell Scripts with Python

#14

The key is knowing what to use where. Writing certain scripts in Bash is miles more efficient use of time than using python because it's "better". While it's certainly possible to write bad scripts in Bash like the example given, it's just as possible to write bad scripts in Python. All the time spent learning to do bash-like stuff in Python could just as well be spent learning to write better Bash. I think the reada…

There are times writing a bash script it feels like "omg - a programming language would be much easier for this task"...

So I write a tiny program in the lang of my choice and invoke it from that place in the bash script.

The balance between the two tools is great - your advice is spot on for me.

Re: Bashing the Bash – Replacing Shell Scripts with Python

#15

I think the author forgets to mention that the primary reason bash is used is because of its cross distribution support. I known if i write my script in bash it will work on any small linux distro. I think python is a close second, but it still use bash to bootstrap my environment for my real work.

I've been using POSIX shell exclusively, and spent a week or so converting all of my existing scripts to POSIX a few years ago (when Ubuntu switched its initscripts to run under dash instead of bash). Bash isn't available on tiny Linux distros by default, because it's quite large. Many use dash, including most busybox based distributions.

With checkbashisms and shellcheck, it's pretty easy to spot compatibility issues and fix them.

Re: Bashing the Bash – Replacing Shell Scripts with Python

#16
post #9

> I’ve idealized the four steps as separate functions. Does the author know he can write functions in bash? I mean, if he wants to compare programming languages, he should at least compare similar programming styles. Bash is great for OS scripts because it has built-in commands and syntax for dealing with processus and their inter-communication (pipes and stuff). Python has NOTHING about that. If you want to do this,…

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.)

Re: Bashing the Bash – Replacing Shell Scripts with Python

#17

The key is knowing what to use where. Writing certain scripts in Bash is miles more efficient use of time than using python because it's "better". While it's certainly possible to write bad scripts in Bash like the example given, it's just as possible to write bad scripts in Python. All the time spent learning to do bash-like stuff in Python could just as well be spent learning to write better Bash. I think the reada…

I work with a couple people who insistently write shell scripts in Python. I know it's anecdotal, but to me, it's like the metaphor 'When all you have is a hammer, everything looks like a nail.' The scripts use functions liberally, but the code isn't DRY nor self-documented. Written in shell, it would take a fifth of the LOC because there's no need to use subprocess or Popen and parse the output. They're not the best developers, but I certainly trust their Python code than I would trust their shell code. And, there's plenty of work to go around, so it's better than nothing.

Re: Bashing the Bash – Replacing Shell Scripts with Python

#20
post #16
post #9

> I’ve idealized the four steps as separate functions. Does the author know he can write functions in bash? I mean, if he wants to compare programming languages, he should at least compare similar programming styles. Bash is great for OS scripts because it has built-in commands and syntax for dealing with processus and their inter-communication (pipes and stuff). Python has NOTHING about that. If you want to do this,…

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…

You 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.
Post reply on HN