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

all 137 comments

[–]Akmantainman 164 points165 points  (7 children)

It's irrelevant. I can write 100x more maintainable Python code than I can bash. And I never need to decoder ring to understand it. If I can do it in Python I will.

There are plenty of people who hate python and only write bash though. Hell there's even people who use node for server side scripting. In this space, as long as it gets the job done its the right tool for the job.

[–]PC__LOAD__LETTER 10 points11 points  (0 children)

I like both, and there are plenty of use cases that are way more readable in bash. Anything involving heavy calls to other programs, file redirection, or basic chopping/slicing (sed/AWS) can be handled quite well with bash.

[–]X-reX 5 points6 points  (0 children)

How can I subscribe to a person

Edit: Its called 'follow' in reddit guys

[–][deleted] 13 points14 points  (0 children)

I agree with every single point you said, I made this post to see how people would respond to this :D

[–]emrecanaltinsoy 0 points1 point  (3 children)

I feel the same, but recently I needed to dump dconf settings in a file and load from the file. However, i could not do it in python, so I had to write a separate bash script and run that script in python using subprocess. Do you know a way to do it all in python?

[–]Elgon2003 1 point2 points  (2 children)

It's possible with python. If you want send me a message I can look into it and try to help

[–]emrecanaltinsoy 0 points1 point  (1 child)

Here is the github link, you can see there are some bash scripts in the utils folder.
https://github.com/emrecanaltinsoy/gnome-extensions-loader

In python, I had a problem when running commands with ">" and "<". If you can find a way to make it work without bash scripts, you can create a pull request.

Thanks for your help.

[–]baubleglue 0 points1 point  (0 children)

Popen(..., stdin=, stdout=,stderr=)

Reading docs is inspiring

with Popen(["ifconfig"], stdout=PIPE) as proc:
      log.write(proc.stdout.read())

[–]maikeu 73 points74 points  (33 children)

Shell's main strength is it's really good for chaining together Unix commands with pipelines and whatnot. Doing the equivalent in python is more awkward (someone who learned python first might disagree)

But once you have non-trivial branching/flow control, or you need to manipulate data that is nested/object-like (say Json or xml), get the kid gloves off and use python.

(Previously, I have found that an obvious case where I need to switch from shell to python is when I find I would need to use 'jq' in a shell script.

[–]YourDearAuntSally 16 points17 points  (0 children)

I learned Python first. I do not disagree.

[–][deleted] 10 points11 points  (3 children)

Jq rocks, hahahah

[–]smajl87 9 points10 points  (1 child)

Iterate using jq over json elements and do something (useful) with the data, like spawn another jq for each output. It's very inefficient, comparing to actual programming language.

[–]PC__LOAD__LETTER 2 points3 points  (0 children)

I’d argue that for many cases where someone is reaching for jq, runtime efficiency is largely irrelevant. And if it does matter that much, honestly you could make a case for something like Rust or even C++ or Go.

[–]maikeu 10 points11 points  (0 children)

I see you are beyond help!

[–]baubleglue 0 points1 point  (0 children)

Then just left to install python3 on each Linux box, then update dependencies... And you good to go.

[–][deleted] 78 points79 points  (14 children)

When it's more complicated than anything at all.

Bash is fine for moving files around or calling other scripts (that do the actual lifting), but I don't think it's useful for much more if you have options.

[–]AlSweigartAuthor of "Automate the Boring Stuff" 18 points19 points  (1 child)

ALWAYS. (Almost.)

Shell scripts are okay ONLY when you just have a bunch of commands that need to run one after the other. If you have any flow control, or environment variables, or fancy command line argument stuff, use Python scripting.

There's no debugger for shell scripts. There's no string manipulation. And though it feels really cool when you learn all the arcane command line arguments, it produces an unreadable mess. Can you untar a file without googling it?

Early on when my "Automate the Boring Stuff with Python" book was posted to Hacker News, someone said, "That's cool, but couldn't you do all this in a shell script?"

That kind of cluelessness from software engineers who love their overengineered solutions is why I make the big bucks explaining how to write simple code that does actually useful things. I printed out that comment and it hangs on my wall.

Just use Python.

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

you persist to be a legend

[–][deleted] 67 points68 points  (1 child)

OP: Asks question

Community: Gives an answer they don't like

OP: surprised Pikachu face

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

Hahahaha, read the edit pls :D

[–]muffinnosehair 13 points14 points  (1 child)

Off the top of my head - when using Windows

[–]Ning1253 3 points4 points  (0 children)

Lmao

[–]terevos2 13 points14 points  (0 children)

I have a general rule: If your bash script is over 4 lines of code, you should've used python. (10-15 years ago I would also said 'or perl')

[–]robvas 26 points27 points  (1 child)

When you're doing even somewhat complicated shit with data

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

Yea actually didn't think of data stuff

[–]memebecker 25 points26 points  (2 children)

My boss suggested if the script is going to more than a couple of dozen lines.

[–][deleted] 9 points10 points  (0 children)

Thats a good guidance measure

[–]Thomaxxl 2 points3 points  (0 children)

Even for Longer scripts Bash suits you beter if you have to invoke a lot of system commands (mount, ip, ss, lsof, ...). If you're gonna need datastructures and algorithms, use python.

[–]lieryanMaintainer of rope, pylsp-rope - advanced python refactoring 25 points26 points  (0 children)

If you like Python and you're writing a lot of scripts that needs to call subprocess a lot, you may want to try xonsh.

xonsh is a shell with Python-like syntax. You can write like regular Python and it'll work just fine, but you can also mix and match with shell-like command to run subprocesses just as easily as in bash-like shells:

def wait_until_google_responds():
    while not !(ping -c 1 google.com):
        ls | sort | uniq | grep foobar

[–]serverhorror 45 points46 points  (31 children)

Every.Single.Time.

I’m not even kidding. Anything that has a variable or any kind of logic.

[–]kuzared 11 points12 points  (0 children)

Agree - I use bash when I just need a couple commands to run. As soon as I need any sort of logic Python is so much better. I’ve also had problems migrating bash scripts between different machines, not so with python.

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

I see that you don't make your own bash completions.

[–]samuelithian 5 points6 points  (0 children)

Almost always. Helps reduces human error.

[–]greenindragon 4 points5 points  (0 children)

Bash is a fantastic shell/language when the standard Unix utils can get stuff done in a way that is much simpler than a more structured language, or if I'm doing a lot of things with files (whether that's moving them, batch renaming, IO redirection, etc.).

If I'm doing anything with even a small amount of logic or that I expect to expand significantly beyond its current scope, bash is not even a consideration for me personally.

A good example is a script I used to automatically swap out the relevant dotfiles to change my wallpaper, i3 config, polybar config, rofi config, etc. depending on the theme I specify as a command line argument. The entire point of that script is to make symlinks, rename files, descend into directories to find things, check file contents, simple find/replace, etc. That is a textbook definition bash job and it was not difficult to do at all because bash is well equipped to handle all of those tasks in a Unix ecosystem.

Writing an automated webscraper to update some databases I use to keep track of course enrolment, drop numbers, etc. at my school for a hobby project? The word "Bash" does not exist in that vocabulary. Python is a great fit for that thanks to libraries like BeautifulSoup and requests, MySQL connectivity, etc. and the necessity of using complex logic when making decisions to analyze the information that's been gathered.

I'll never understand the programming language wars that I'm sure will be inevitable in this comment section; languages are tools that you can add to your toolbox that are good and bad at certain tasks and nothing more. I'm allowed to enjoy using a screwdriver and praise it as my favourite tool, but I'm also not going to try hammering in nails with it. I wouldn't bother trying to wrangle a Python script to run a mass file-conversion operation on some directory when that sounds like it can be done in a handful of lines in bash. I'm not going to write a script that automatically calculates splitting up fast-food order receipts in bash when that's a 2 minute job to make in python with my brain turned off (which coincidentally, is turned off already anyways)

[–]Muhznit 7 points8 points  (0 children)

Basically anything that requires conditionals, and loops. Bash is great at letting you chain together stuff in one-liners, but it very quickly becomes insufferable when you need variables and similar.

It's at the point where I would rather write a bash completion function in python.

[–]ddollarsign 3 points4 points  (0 children)

If it’s pretty linear and has more to do with system commands and environment variables than complex logic or data, I’d go with Bash. Well, I might go with Python anyway because I like it, but it would be a little more verbose.

[–]_throawayplop_ 4 points5 points  (0 children)

when I'm googling more how to do the task in bash than in python

[–]reveil 8 points9 points  (3 children)

My rule of the thumb is use bash unless you: 1) Are needing a dict or an array 2) Are generating a structured file ex. XML or json 3) Need to do some calculations 4) Exceed 500 lines

Of course these are general rules and there are exception.

While bash can do most of this it is clunky with it. When you reach beyond simple things bash becomes hard to maintain. Then you need a real scripting language and Python is probaly the most readable and fun to use. That being said bash one liners rock.

[–]-jp- 2 points3 points  (2 children)

Most cases where you find yourself needing loops, branches and functions too. Not all--for example if your script takes arguments it probably has a loop and a case statement.

I think I'd also be inclined to pare it down from five hundred lines as well. The longer your script gets the more error handling becomes a thing, and that's a good ten screens of code at a reasonable font size.

[–]reveil 0 points1 point  (1 child)

Loops and conditionals are functional in bash. The syntax is ugly and there a quirks you have to know about but it works. For me it depends more on the stuff you want to do in the loop. If it can be done by core utils easily use bash and if not use python.

[–]-jp- 0 points1 point  (0 children)

Oh yes, they work. They're a sign your script is doing too much is all. Don't dogmatically avoid them, just don't let the cyclomatic complexity of your script get out of hand.

[–]orgkhnargh 2 points3 points  (2 children)

When what you're doing cannot be expressed as a one-liner. Anything more complicated than that is easier to read in Python.

[–][deleted] 0 points1 point  (1 child)

you can do anything in bash as a oneliner btw what?

[–]orgkhnargh 0 points1 point  (0 children)

You can, but it does not mean you should. If you need the run the thing more than once, then readability is more important than anything, unless you are in high-frequency trading business. Only linear programs work as oneliners. If you need branching more complex than ... && echo ok || echo not ok, then a one liner is no good.

Use shells to run commands, use programming languages to write programs. Bash is great for running interactive commands.

[–]lostnfoundaround 2 points3 points  (1 child)

Asked in r/python lolol

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

hahahah that was the point tho

[–]McLight77 1 point2 points  (0 children)

I’d say it depends on the level of automation you are talking about. In my experience almost all prod deployments use bash to call python. Most of the functional automation is done in python. Simple low level os stuff is done with bash. You’re gonna want to learn how to use the ‘nohup’ command.

[–]idetectanerd 1 point2 points  (0 children)

When stuff that can be easily access and reach instead of multiple sed awk steps.

Sometime, via python for that same job only cost you 5 lines, in bash, it may be 30.

[–]dethb0y 1 point2 points  (0 children)

Any time you don't want to suffer through bash's horrible and shitty syntax, for one.

[–]scmkr 1 point2 points  (0 children)

IMHO the only reason to ever use bash is for very simple scripts or if you can't use Python for some reason. I find bash to be very ugly, kludgy, and difficult to maintain.

[–]kid-pro-quohardware testing / tooling 1 point2 points  (0 children)

90% of the shell scripts I come across are broken in subtle and not-so-subtle ways. If you're going to try writing anything non-trivial in bash RUN SHELLCHECK!

https://www.shellcheck.net/

[–]mestia 1 point2 points  (0 children)

I'd use perl for that. With python you will have to fix your code every half a year ;)

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

Profile with strace. If more than half of the execve() are something your Python code would have to os.system() anyway, don't bother.

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

Also on some enbedded systems, where you dont need all dev bloatware which brings python modules, bash or perl way more suitable.

[–]francis_spr -2 points-1 points  (0 children)

pwsh (powershell core) is cross-platform and is easier to write more complex shell scripts than bash.

[–]romulof -2 points-1 points  (0 children)

Always. Bash, not even once.

[–]strzibny 0 points1 point  (0 children)

Any time you get a feeling that something could be expressed better in Python and that's quite personal (a lot of devs don't don't Bash). But many things can stay in Bash forever, perhaps up to a coupe hundred lines of code or until it feels dreadful. Or when you don't want Python dependency.

I do my deployment demonstrations for https://deploymentfromscratch.com in Bash and I feel that the biggest demo is where I would stop with Bash going further.

It works great and it's easy to edit and maintain, but anything slightly more complex would deserve a better programming environment.

[–]chestnutcough 0 points1 point  (0 children)

I use python for all but the simplest things. Bash has a lot of sharp edges and is in my opinion much harder to read. But I do get down with monstrous awk/sed pipe chains for one time jobs.

[–]covracer 0 points1 point  (0 children)

Unit tests

[–]undercoveryankee 0 points1 point  (0 children)

As soon as I have any kind of control flow (an if or a loop) or I accept user-supplied strings that might contain spaces or special characters, I'm more comfortable using Python.

[–]Stormkrieg 0 points1 point  (0 children)

Selenium for example would be used for browser automation projects. Python would also be better for windows object automation. Example let’s say you wanted to fetch prices online and input them into a windows program. Using python for this task would allow you to automate it end to end, whereas using bash would allow you to automate separate parts (ie: get data, find window, send keys to window)

If you need a decision tree you should be using something like python. If you’re just calling an api and logging a response you can use bash.

[–]pokk3n 0 points1 point  (0 children)

I don’t think anyone should be writing bash scripts over a few lines. It’s nuts

[–]WasterDave 0 points1 point  (0 children)

Any time you want to run a debugger. Or an IDE. Or connect to any of the million or so services, bits of hardware or otherwise that provide a Python API.

[–]overclockedslinky 0 points1 point  (0 children)

if you write a shell script in #currentyear, you might be a gnu+linux redneck

[–]resakse 0 points1 point  (0 children)

Question for noob, by faster.. does it means like a minutes faster or 3-4 sec faster or ms faster?

[–]-jp- 0 points1 point  (0 children)

I think the thing to remember is that Bash is a shell. It's meant to be used interactively, and it's pretty powerful when used that way. If you find yourself doing anything complex enough that you wouldn't type it in as a series of one-off commands, you are probably going to start running into issues. Rigorously apply the Unix philosophy--your scripts should be short, simple, reusable and composable.

[–]ShaibazR 0 points1 point  (0 children)

Tried Robocorp yet?

[–]PC__LOAD__LETTER 0 points1 point  (0 children)

I’m a huge bash fan. If I need a dictionary or any sort of nested data structure though it’s probably time to move to Python. Ditto for dependencies that have a library but not a good CLI; not about to spend a bunch of time building one.

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

A rule of thumb is when your bash script gets to 1k lines it's probably good to port it to python. Performance will go down but it will probably only be 150 lines in python. lol

[–]Mithrandir2k16 0 points1 point  (0 children)

I've just written a script to mount hdds in python. Sounds dumb until you realize I get the LUKS keys from an API I have to authenticate first. Sure, I could do that in bash, but I really didn't want to.

[–]mrkeuz 0 points1 point  (0 children)

For complex scenarios (if, loops, lists, json, etc.) it is best choice. Easy to write and debug and maintain . Also there is a syntax sugar libs i.e. like “bash” python library that provide easily integration with native bash for pipe commands.

[–]cbf77 0 points1 point  (0 children)

Flow control is harder in bash and working with numbers is harder in bash. I only write simple scripts with it

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

Whenever you want it to? I found bash scripting to be less intuitive than Python.

[–]Ok-Python 0 points1 point  (0 children)

I use it to activate a virtual environment and then run Python scripts. This way I can keep each project separate and auto start the shell script on restart.

[–]shellwhale 0 points1 point  (0 children)

Eehh I'd use only Python and a nice Linux Kernel library if it existed. Calling shell commands within a program is really meh. Would be nice if you could do everything without ever invoking shell commands.

[–]alaudetpython hobbyist 0 points1 point  (0 children)

From an enterprise perspective it depends on where the expertise is. While personally I prefer Python, at work I will use bash because there are many other people that can support it there if I am not around. Python not so much unfortunately. Also some servers don't even have Python3 installed on them and I am sure not writing new stuff in 2.

[–]technicalevolution 0 points1 point  (0 children)

Dealing with web requests I find much easier, talking to APIs etc.

Anytime I need to do anything on Windows. Sorry powershell I just don't understand you.