Hacker News new | past | comments | ask | show | jobs | submit login
Bash Scripting: Why didn't I start this earlier? (burakkanber.com)
50 points by bkanber on Aug 16, 2013 | hide | past | favorite | 18 comments



I use Python for my scripting needs. It is a little bit more cumbersome to call unix tools from a Python script, but handling strings and arguments and things like that is so much easier. I feel like bash is really weird for anything longer than a for loop.

Can someone please provide some arguments why I should write bash scripts instead of Python scripts?


Personally I use bash when I need to, well, script shell commands. Anything that is reasonably described as series of shell commands, filter/pipes.

There are a lot of best-practices that one need to adapt though, and there's definitively a learning-curve. Generally scripts should work under things like dash, and not be bash-specific. That means delegating some work to sed/grep/awk (or being really "clever").

I've found that using functions, and passing arguments rather than setting (global) variables help a lot -- and if you get to the point that you really want do debug, you're better off rewriting in something like python.

I've never liked perl -- it's always seemed like a bit of a pointless exercise in moving side-ways rather than "up". I've never had to move away from shell scripts out of performance needs, only out of maintenance needs -- and for me perl buys me nothing in that direction.

Now, things like fabric[f] can make sense -- but the benefit of (ba)sh is that it's guaranteed to be there (on a nix platform) -- and it is great for bootstrapping.

I'm not* a fan of how some projects use (ba)sh, though. The ones that list instructions like these (and there have been a few lately):

  curl http(s)://great-tool.com/install | sudo bash
They often a) don't use https and b) require sudo/root blindly (rather than at least wrapping what needs sudo in something like:

      prompt -p "Need superuser permission for apt-yum-install libfoo [y/n]: " ans
      if [ "${ans}" == "y" ]; then
          sudo apt-yum-install libfoo
      else
         echo "Aborted."
         exit 1
      fi
Which generally works rather painlessly -- and could be further "automated" by allowing a --yes-go-ahead, or checking if the script is running as root.

[f]: http://fabfile.org


If you are worried about dash compatibility I hihly recommend checkbashisms:

http://packages.debian.org/sid/devscripts


My experience is that bash is faster and easier for below 20 lines. But if things grow, you'll wish you had used a real scripting languages.

All useful tools grow.


It's not really about LoC, it's about what you're trying to do. If what you're doing is, essentially, job control, then Bash is still easier than Python. But not for much else, and Python is very widely available these days.


My experience is that job control in Perl and Ruby is almost as easy as bash, plus you have a real programming environment to fall back on.

My experiences with job control in Python were admittedly not quite as pleasant.


Same here, I could never relate to bash's specific syntax, so I just started to do everything in Python. I'm thinking about fully switching to Julia for this though, as it offers an easier way of integrating Unix commands.


As a dual wielding hat person (sysadmin and programmer), stay away from bash programming.

Use a modern scripting language like Python or Ruby otherwise you are entering a world of pain.

http://mywiki.wooledge.org/BashPitfalls


Dependencies are another huge issue if you want to be cross-platform. A Ruby script (for example) relies on the Ruby interpreter, which works the same cross-platform. If it has other dependencies, there's an established way to declare and install them.

In contrast, a bash program calls out to lots of little programs: sed, awk, grep, etc. Different platforms have different implementations which take different flags, etc.

My friend and I just spent a couple of days getting some bash scripts that ran fine under Cygwin to also run on OSX. It was painful.


Ruby system scripts assuredly do not work the same cross-platform, particularly if they have to call to C libraries. Expect to have to rejigger them just as you would bash.


I found PowerShell quite capable as both a shell and a programming language, scaling up to larger scripts and yet remain readable.

Pash sadly isn't complete enough to be used, though.


welcome to 2003


Author makes that point in the very first paragraph.


In the title, even!


if you're going to make a snide comment, shouldn't you get the years correct? I'm pretty sure bash has been around since the late '80s or early '90s.


My point was not when bash was created, rather good systems people have been doing this very thing for quite some time. I think I'm just tired of reading ZOMG blog posts. My bad.


Bash > All other languages. Bash is glue.


you thinking of perl




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: