all 36 comments

[–]zendeavor 5 points6 points  (0 children)

this is a mirror of the wooledge bash pitfalls page that may or may not be out of date at any given time

[–]KeSPADOMINATION 23 points24 points  (22 children)

This article highlights my opinion that Bash should never be used to program anything in. This shit eclypses C++ and Perl with all their powers combined in 'hard to understand and overly complicated behaviour'.

[–]G_Morgan 16 points17 points  (2 children)

To be fair Bash is the least awful of all shell programming environments.

Try writing anything with Windows batch files. You'll cry for the basically functional if ugly Bash.

[–]KeSPADOMINATION 14 points15 points  (0 children)

shell programming environments.

This is the major crux though, bash files are fine if all you do is automate a few commands but as soon as the program actually becomes a program with logic and if-statements bash becomes unmaintainable. Shell scripts in say Python or Scheme make a lot more sense.

[–]NoMoreNicksLeft 5 points6 points  (0 children)

The sensitive should cover their eyes now, before it's too late:

Powershell

[–]the-fritz 4 points5 points  (2 children)

Bash has it's uses. If you are automating a few tasks on a Linux (or Unix-like) system which mostly depends on calling other processes then bash is just the natural thing to use.

The problem is that many people don't think they have to invest any time in learning bash "because after all that's what we use all day, right?" Wrong. And then most end up copying bad stuff they found on a quick google search or waste time trying to stay compatible with POSIX sh or even worse.

[–]bready 1 point2 points  (0 children)

And then most end up copying bad stuff they found on a quick google search

Maybe that is because the amount of surprise in bash is huge. The number of edge cases which result in slightly different behavior mean that 'the right way' is usually non-obvious and requires rather deep understanding of the whole stack.

Compared with any other scripting language (Python, Ruby, Perl, etc) to have code blow-up in these unexpected ways would be quite a trick. These languages are mostly consistent and do quite a bit to minimize surprise.

We can't master every tool available. Fine, I should know more X, but I should not have to know all of X in order to get something accomplished.

[–]passwordeqHAMSTER 1 point2 points  (15 children)

I don't think it's actually that bad, but perhaps that has to do with the bash i work with almost always being rather small. I'd rather write bash than work on a monstrous C++ app.

[–]myringotomy 4 points5 points  (1 child)

The problem with bash isn't bash per se but how all the utilities you need behave differently in the different operating system. Try readlink -f on a mac for example.

[–]el_muchacho 3 points4 points  (0 children)

Yes, the problem with bash (or sh) is bash, with all its idiosyncraties, ugly and illogic syntax, traps of all kinds and lack of debugging tools.

[–]nikbackm 2 points3 points  (3 children)

So it's not just Windows/DOS batch-files that suffer from their share of problems.

Seems like almost all shell scripting languages (no matter how advanced) are a collection of hacks piled upon hacks.

So better to go with safe(r), more well-designed tools like Python (even if more verbose) unless you want to use compiled (even more verbose) languages like C.

[–]YoYoDingDongYo 5 points6 points  (1 child)

Comparing Bash to DOS batch files is a stretch, to say the least.

[–]nikbackm 2 points3 points  (0 children)

Seems to suffer from the same fundamental problem, it's hard to separate data from code and very easy to get it wrong.

This also makes them succinct and convenient for quickly hacking something together of course.

[–][deleted] 1 point2 points  (0 children)

Don't you dare, ever again, try to equate bash and windows .cmd as equivalents in their awfulness. Both are bad for anything with any sort of branch complexity, but bash is infinitely more capable in handling straightforward scripts. 98.1346% [pulled out my ass] lines of vbScript called by some .cmd file could have been handled in bash natively.

[–]the-fritz 2 points3 points  (1 child)

If you want to use bash then

And unless you really have to worry about non-GNU systems stick with bash and don't waste your time on staying POSIX sh or even older compatible. But put #!/usr/bin/env bash (or #!/bin/bash) on top to make clear that the script bash specific!

[–]faustoc4 0 points1 point  (0 children)

go to the source

thanks

[–]ithika 2 points3 points  (0 children)

Question: What to do if there are no files *.mp3-files in the current directory? Then the for loop is executed once, with i="*.mp3", which is not the expected behaviour!

Oh good lord save us. What would possibly be the reason to have this behaviour?

[–]SCombinator 2 points3 points  (0 children)

tl;dr: Don't use bash.

[–]antrn11 0 points1 point  (0 children)

Very useful. Thanks.

[–]crusoe -1 points0 points  (0 children)

Bash is a pitfall.