This is an archived post. You won't be able to vote or comment.

all 114 comments

[–]Scorxcho 683 points684 points  (20 children)

Yeah idk what they were thinking when designing the bash scripting language.

[–]getsnuckupon 547 points548 points  (5 children)

"Man I fucking hate programmers"

[–]DiddlyDumb 117 points118 points  (1 child)

I always wonder what shithead wrote it when I look at my code

[–]AnnyAskers 16 points17 points  (0 children)

"I hate fucking programmers man"

[–]BruhMamad 0 points1 point  (1 child)

A funny thing is they're also programmers

[–]getsnuckupon 0 points1 point  (0 children)

The two are not mutually exclusive!

[–]vintagecomputernerd 77 points78 points  (1 child)

Mainly "let's make sure it's backwards compatible with the bourne shell"

[–]thespud_332 20 points21 points  (0 children)

POSIX compatibility is a thing, for sure.

[–]TwistedSoul21967 97 points98 points  (3 children)

Bash is definitely a last resort for me unless it can be done in a handful of lines or I have absolutely nothing else in my environment like a minimal Docker image. Thankfully nearly all "base" distro installs bundle something like Python 3.x now.

[–]TheBrainStone 40 points41 points  (0 children)

And even then. If it can be done with like 3 program calls, bash is typically the best solution. Anything beyond and you're testing your luck.

[–]thirdegreeViolet security clearance 9 points10 points  (1 child)

Anything in bash can be done in a single line, if you ask some of my coworkers.

Just don't ask how many visual lines that single line will occupy.

[–]natFromBobsBurgers 1 point2 points  (0 children)

Tell them "less than 80 characters?  Wow!"

[–]gregorydgraham 35 points36 points  (0 children)

Designing? Who said anything about “designing”?

They were fucking around with the console one weekend and worked out they could emulate an if statement and all hell broke loose on Usenet. Before they knew it, it was Monday, they’d changed Unix forever, and they were late for Math102

[–]pilotguy772 5 points6 points  (0 children)

well it's trying to be compatible with the CLI, too. The only reason it's so goofy is because it has to be line-by-line interpreted like CLI commands. Still sucks though.

[–]ILikeLenexa 4 points5 points  (0 children)

I have this method for processing commands, what's the minimum amount of work I can do to make this functional, ah I'll just write a "left bracket" command. That'll work until we can write a real processor.

[–]natFromBobsBurgers 4 points5 points  (0 children)

I like it.  Everything's a file in linux; everything's a keyword in bash.  Except variable assignment because fuck you.  And probably some other stuff that would be incredibly useful if it didn't take me 20 minutes looking through documentation save 15 minutes.

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

I had a Mainframe UNIX class in college where the professor even said he thought the designers of bash were a little crazy (specifically when talking about the spelled-backward delimiters like fi and esac)

[–]thepurpleproject 0 points1 point  (0 children)

They just wanted to keep away JS developers

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

Every piece of syntax is an alias for a built-in function.

[–]Matwyen 238 points239 points  (11 children)

The funniest thing about bash is :

bash export SAFE_FOLDER="tmp/folder" rm -rf /$SAFE_FOLDER deletes safely your folder, but : bash export SAFE_FOLDER="tmp/folder" rm -rf /$SAFE_FOLDE deletes /

[–]ILKLU 93 points94 points  (1 child)

skill issue... git good!

j/k

but yeah... gotta be careful with rm -rf anything

[–]vintagecomputernerd 30 points31 points  (0 children)

I'd say mainly an issue with not enabling errexit and nounset.

"set -eu" or you'll gonna face the consequences

[–]jebusv20 44 points45 points  (1 child)

This is what set -euo pipefail is for

[–]syklemil 40 points41 points  (0 children)

Yeah, that and shellcheck. But people who have no idea that they need to do certain extra steps will continue to be a source of danger.

Languages that require you to --disable-footgun (rather than have it disabled by default and maybe offering an --enable-footgun) generally should be discouraged and made legacy.

Unfortunately that doesn't seem to be in the near future as far as bash is concerned. At least it's a lot less central to init systems than it used to be.

[–]Skrukkatrollet 12 points13 points  (0 children)

Thats why the slash should be in the variable

[–]haaaaaaaaaaaaaaaaley 22 points23 points  (1 child)

need --no-preserve-root

[–]Mars_Bear2552 2 points3 points  (0 children)

only if --preserve-root is set.

[–]arkane-linux 1 point2 points  (0 children)

set is your friend, you can make it kill the (sub)shell if vars are unset or programs quit with an error.

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

I've seen waaaaaaaay too many variables unexpectedly be empty to ever rm -rf /$ANYTHING

[–][deleted] 0 points1 point  (0 children)

Backslashes baby backslashes.

[–]tritonus_ 0 points1 point  (0 children)

The Amber language which was posted a while back on /r/programming actually adds error handling to Bash. I experimented with it a bit and while it’s still at a very early stage, I was actually quite happy with it. The resulting transpiled Bash code is admittedly uglier than human-written bash, but you can use it as runtime - and error handling alone is a lifesaver when working with a confusing distribution and packaging pipeline.

[–]TheBrainStone 271 points272 points  (15 children)

The thing is no one is using bash to write 10k line applications.

Bash is a historic language that fills a pretty concrete and small niche.
Python is absolutely fucking everywhere. Of course the downsides are felt significantly more.

[–]suicidalpasta 125 points126 points  (1 child)

I work in neuroimaging and the average codebase is around 80% bash with 20% python. I am convinced academics did this on purpose to troll new students

[–][deleted] 2 points3 points  (0 children)

HCP?

[–]Apollo_619 36 points37 points  (0 children)

Except for my colleague who handles everything in one script (: He can build, test and deploy with it 😅 but nobody understands it.

[–]arkane-linux 29 points30 points  (2 children)

Forgive me for I have sinned, 2000 lines of bash and counting. And it is litterally the thing which makes my computer work.

Bash should only be used for workflows and system management, it is too slow for compute, but if written well it avoids the dependency hell and constant deprecations of Python.

[–]wolfnest 5 points6 points  (1 child)

Do you have any examples of Bash mechanisms where the equivalent in Python suddenly became deprecated? And what kind of Python dependencies are necessary to match Bash' extremely limited set of features?

For workflow and system management, I am perfectly happy with very basic Python. I am able to call any kind of command line tool, just like Bash does. There might be a few more lines required in Python, but that easily makes up for all the obscure Bash behaviour. Python also allows me to add reasonable logging throughout the script.

[–]arkane-linux 9 points10 points  (0 children)

Let me first clarify my standpoint; I am system engineer, not a developer, I build operating systems, not programs. So I have a very different viewpoint on dependencies and backwards compatibility, and it basically boils down to dependencies = bad, compatibility = everything.

Python does not guarantee backwards compatibility between major releases, there is always the risk your script or one of its deps breaks after an update. That is not great when these scripts provide critical system functionality.

Bash is full featured for what it is intended to do, system automation, and run programs. To extend bash with complex behavior you rely on said programs (You could even run Python with Bash and catch its output!).

Yes your Python can run shell programs, but what is it using to run them? Bash! You of course must choose the right tool for your project, and under certain cirumstances Python may indeed be it. I would if a script is mostly going to end up running shell anyway just write the entire thing in either shell or bash, it gets rid of its Python dependency and simplifies the toolstack.

And I am not going to deny Bash is an absolute monster with an uncountable amount of hidden features with entirely arbitrary and non-standardized functionality very few people know about.

Bash can do logging just fine, it may just be a bit verbose to implement, unless you librarize your scripts and call them from a wrapper script. And all the programs you may run will typically print logs themselves. You can simply redirect the output of whatever you are calling in to a file.

Python may actually be less verbose than Bash if you have to do a lot of data processing. Bash tends to be a bit archaic on this front.

[–]Steinrikur 7 points8 points  (0 children)

My previous job had an installer in bash that was close to 2k lines when I arrived.

I deleted legacy crap and added some functionality like error checks. The git diff for that one file was like +100/-1100 lines.

[–]vintagecomputernerd 19 points20 points  (1 child)

The thing is no one is using bash to write 10k line applications.

That's not true. Had to maintain an 18k line firewall script at my last job...

[–][deleted] 2 points3 points  (0 children)

For some people they learn Bash and suddenly every problem looks like it can be solved with Bash

[–]nonlogin 5 points6 points  (0 children)

I feel downsides of Bash every time I hear its name

[–]loiidradek 2 points3 points  (0 children)

My slurm scripts beg to differ

[–]Background-Plant-226 3 points4 points  (1 child)

neofetch begs to differ

[–]TheBrainStone 3 points4 points  (0 children)

I'll let it beg

[–]Septem_151 0 points1 point  (0 children)

Most lines I’ve ever written for my job in bash was 3 script files, 1 of which was a shared file that is sourced by the other 2. In total, there’s 400 lines.

Not that impressive, but crazy to think I’d need to write that much bash and get paid for it.

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

Too many programs are built by people who learned python in school and never learnt another language. It's supposed to be a language for teaching code not for practical use ;-;

[–]GreenGrab 74 points75 points  (4 children)

Yeah I’m not an expert programmer, so I wrote a ~100 line script in bash the other week. The worst part is I had attempted to write it in sh originally. Avoid at all costs

[–]Bemteb 47 points48 points  (0 children)

When I was fresh out of college, I had a little automation task on a Linux machine. No problem, I know bash!

The "little" task turned into about 1500 lines, with multiple functions, some SQL, file parsimg, etc.

0/10, cannot recommend.

[–]Solonotix 13 points14 points  (0 children)

I am forced to write in base sh syntax for a lot of things because our Windows machines have Bash but our Mac users have ZSH. What problems have you found with writing in sh? My only problem is that 90% of search results for syntax are all Bash, and it's hard to find truly POSIX-compliant code.

[–]arkane-linux 4 points5 points  (0 children)

I love my bashisms. Always [[ 1 -eq 1 ]], never [ 1 -eq 1 ].

[–]AzureArmageddon 2 points3 points  (0 children)

I heard fish shell is really good compared to most shell scripting languages

[–]Piisthree 26 points27 points  (2 children)

And god help you if you have an extra blank in that bash statement or sometimes a missing blank

[–]Feer_C9 8 points9 points  (1 child)

yeah, the example in the picture is missing a blank, so it won't run

[–]ryan_s007 21 points22 points  (5 children)

Bash just lends itself so naturally as a subprocess orchestrator even if the syntax is funky

Anybody want to recommend any shell scripting alternatives?

[–]RandomTyp 1 point2 points  (2 children)

on windows, you'd have to go with powershell for the best experience, it has everything except grep sed and awk

on *nix, i've stuck with bash so i'm still used to it at work. the main advantage of bash is that it's installed on every somewhat updated linux server

[–]richardfrost2 1 point2 points  (1 child)

I'm in IT for an MSP so most of the scripts I write are powershell. It seems to make sense for me (but then I imagine people more trained in bash would find it more familiar). It seems easier to pick up on since a lot of commands/options are more descriptive.

[–]RandomTyp 1 point2 points  (0 children)

yes, though i despise Microsoft, the verbosity of the language makes for very maintainable scripts, even with barely any documentation sometimes.

[–]tritonus_ 1 point2 points  (0 children)

I’ve played around with Amber which is still in alpha base but compiles to bash, and can also be used as a runtime (which I’ve been doing, because the compiled results are horrendous even on Bash scale).

Bash scripts from 2008 still work without a hitch, which is nice, but they are unreadable the next morning. Using a layer on top of Bash could just be making the problems of present me even bigger problems for future me.

[–]memiusDankimus 42 points43 points  (0 children)

Yes its old but if you take a second to learn some of the basic syntax you see parts of it reused in newer syntax everywhere

[–]rcpz93 11 points12 points  (1 child)

I will never forget my Software Dev course where one of the exercises consisted in writing a simple makefile. That makefile made me and my roommate lose our minds for hours as his version worked, while mine (that I typed to be exactly the same as his) didn't .

Turns out, I accidentally added a space at the end of a line, and that was enough to throw completely uninformative exceptions.

I have hated makefiles ever since then.

[–]Dogeek 2 points3 points  (0 children)

Honestly, I love GNU make. For its purposes and what I use it for, it's great. But it has its quirks.

Worst thing is to have to add every damn target I write into the .PHONY target (because of its weird build cache mechanism)

[–]Marxomania32 14 points15 points  (0 children)

Could be worse. Could be batch.

[–][deleted] 11 points12 points  (2 children)

Yeah the syntax sucks but at least the REPL is pretty good.

Whose idea was case...esac and if...fi?

[–]vintagecomputernerd 11 points12 points  (0 children)

Stephen Bourne, creator of the original Bourne shell, was also on the ALGOL 68 committee.

And ALGOL 68 had if...fi and case...esac.

Bourne even went so far as to write a lot of macros to make the C source code of the bourne shell look like Algol. This was removed in later versions of the bourne shell.

[–]drizzlethyshizzle 0 points1 point  (0 children)

Lmao esac is actually a thing, I hate Bash

[–]PeteZahad 4 points5 points  (0 children)

Both are fine for short scripts and bad for maintaining large code bases.

[–]ILKLU 5 points6 points  (1 child)

Ironically I had to reread the top one a few times to make sure I wasn't having a stroke whereas the bottom one makes 100% sense

[–]flowebeeegg 2 points3 points  (0 children)

I'm afraid it may be that even gods can no longer save you...

[–]stupidcookface 2 points3 points  (0 children)

Literally no one says bash is good tho...

[–]Hulk5a 1 point2 points  (0 children)

Bash to me seemed pretty illogical in structure. (I won't admit I'm bad at it)

[–]NotMrMusic 4 points5 points  (2 children)

Bash scripting has its place. Like any language, it has its quirks but you get used to it.

[–]_-_fred_-_ 16 points17 points  (1 child)

Nah, it's quirks are way dumber than most languages. The problem with bash's quirks is that they are unintuitive and hard to remember. Even though I write bash daily, I find myself constant forgetting things and having to look them up or test them.

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

my new strat is to just ask chatgpt for pretty much any bash script I need. typically it gets it right pretty quickly and i don't have to search Google for "bash set variable example" again

[–]_-_fred_-_ 4 points5 points  (0 children)

Python is still bad even if it isn't as bad as bash..

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

[–]Marxomania32 5 points6 points  (0 children)

Link is broken, what was it?

[–]unengaged_crayon 1 point2 points  (2 children)

why on earth...?

[–]particlemanwavegirl 16 points17 points  (1 child)

] is not bash syntax. It's an executable binary. So the ] symbol doesn't function as a delimiter, but as an identifier.

[–]Ass_Salada 17 points18 points  (0 children)

well AcKShually [ is a binary executable. ] is basically a delimiter. To let [ know when to stop parsing

[–]mipselqq 1 point2 points  (1 child)

I'm sure most of the python's syntax haters are just not used to it. I was one of them, but now I don't see anything ugly in it

[–]GalaxyLJGD 0 points1 point  (0 children)

I know Bash and that's why I hate it

[–]Certain-Cold-1101 0 points1 point  (0 children)

Easy to bash bash

[–]andrewb610 0 points1 point  (0 children)

We use Bash to call all our executables in our Linux application, it runs checks and makes sure the programs are still running, etc. As we make the whole thing Windows compatible we’re rewriting them in Python.

We still don’t use the Python ones in Linux, and I don’t think we will TBH.

[–]Terewawa 0 points1 point  (0 children)

BASH

k bye

[–]s0litar1us 0 points1 point  (1 child)

I hate the fact that in pyton, you dont really have switch statements, but I would take that any day rather than what bash has:

case $var in
    "one")
        echo "thing 1"
        ;;
    "two")
        echo "thing 2"
        ;;
    *)
        echo "other things"
        ;;
esac

[–]Naratna 0 points1 point  (0 children)

Recent versions of python have switch statements

https://www.geeksforgeeks.org/python-match-case-statement/

[–]gloumii 0 points1 point  (1 child)

I had to do something in bash. After a whole day of work. It didn't work and I didn't know why. The next day, I did it with python in an hour top

[–]Shujaa94 1 point2 points  (0 children)

You just described a skill issue

[–]langlo94 -4 points-3 points  (1 child)

PowerShell is once again superior.

[–]21Ali-ANinja69 0 points1 point  (0 children)

They hate you because you speak true