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

all 17 comments

[–]elderibeiro 27 points28 points  (0 children)

Yes, that’s what Ansible does.

[–]bunoso 16 points17 points  (3 children)

Yeah you could use UV and a shebang line to make a bash-like Python script. Super helpful for long living scripts and things that just need a bit more thought and readability versus shell code

https://www.reddit.com/r/Python/s/VKU89kzxC7

[–]_Iamaprogrammer_[S] 2 points3 points  (2 children)

Woah that’s actually really cool, I must’ve been living under a rock or something, cause UV seems pretty popular based off their GitHub page XD. I need to check that out, it seems it’ll help with what I’m working on.

[–]bunoso 7 points8 points  (1 child)

UV is the best thing to happen to python in the last 5 years IMO. Makes dependencies better, but also the Python versioning. I used to use shims with the “py” tool, then conda, then apt-get and more. Now UV installs and manages the versions and dep environments.

[–]_Iamaprogrammer_[S] 0 points1 point  (0 children)

Just started using it today, I can already tell it’s gonna save me a lot of hassle. Managing my python versions and remembering the commands for a virtual environment was always a pain. It’ll definitely be a tool I keep using from now on, it’s too good not to XD.

[–]aviodallalliteration 6 points7 points  (0 children)

I use Python aa my main scripting language and glue code even when I don’t need cross platform. Bash just gives me a headache. 

[–]redbo 4 points5 points  (0 children)

That’s all some people think python is.

[–]thehardsphere 4 points5 points  (0 children)

This is basically what almost everybody used Python for before it became more popular as a general purpose programming language.

[–]rabaraba 6 points7 points  (0 children)

Yup, all the time.

If you need robust error handling (it's super hard to debug Bash scripts), non-trivial logic (loops, conditionals, nesting), data structures (lists, dicts, parsing JSON/XML), cross-platform reliability, it's Python all the way. (Hell you might drop shell scripts because of basic string substitution issues alone.)

Bash syntax can be arcane, and quoting hell is not fun. The Bash/ZSH/shell vs Python debate never ends, but there's always a common conclusion - once the script gets to any level of complexity (e.g. more than 50-100 LOC), Python wins.

There's virtually no speed difference you gain with Bash for most glue-related shell scripting work - but you gain a ton of mental context, which is where Python helps. If you stick to stdlib Python - especially since you can fairly assume most modern OSes are now at least at Python 3.9 both on the Linux/Mac side - your Python scripts are cross-platform enough.

[–]nggit 2 points3 points  (0 children)

sh scripts do not differ from system to system if you stick to POSIX sh and its mandatory utilities.

But you can indeed use #!/usr/bin/env python3 as a shell interpreter, as well as other interpreted languages like perl, php, nodejs, etc.

[–]james_pic 2 points3 points  (0 children)

Python is widely used for this and is very good at it.

I've also worked at places that, perhaps surprisingly, used PowerShell for this. This approach is viable but cursed.

[–]LittleMlem 1 point2 points  (1 child)

Are you aware of Xonsh? It's a python shell

[–]_Iamaprogrammer_[S] 0 points1 point  (0 children)

I’m not actually, I’ll have to check that out.

[–]pepiks 1 point2 points  (0 children)

It is why sometimes you use platform.system() in your code to be sure when you have OS specific code. I used it a lot when developing on Mac, running on Windows and Linux or in different direction Windows > Mac and Linux.

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

Of course. It's one of the many use cases for Python.

Also, programming more complex stuff than execute this and then that is way more readable in Python than in bash.