Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
As far as I know, both are Turing-complete. So, the answer is no.
Another way to see it: Bash is written in C, but there should be no problem writing it in Python. It would be a particular challenge to write a Python interpreter in Bash (of course, Bash could just call Python, but the opposite is also true).
If you want a script to alter the parent shell, leaving you with a changed directory or with newly-set environment variables, you'd need write it in Bash and source it.
The Ranger file manager is written in Python. The component that lets you use Ranger as your cd command? Written in BASH.
If you want a script to leave you with a changed directory, or to leave you with newly set environment variables, you'd need write it in Bash and source it.
Basically, you say that a Python program called from an interactive shell prompt can't change that shell's current directory. In other words, it can't change that shell's memory.
First, let me comment that the opposite is true as well: Run a Python program that issues an interactive prompt and try to change its current directory from a Bash program you launch at the prompt.
Second, I don't know where the current directory is stored, but if I did, there is a fair chance my Python program could change it by accessing /dev/mem and/or /dev/kmem.
Third, I could rewrite the Linux kernel in Python and run a shell on it. Then, chdir is executed in Python.
I suspect that the original poster did not have points 2 and 3 in mind, but the question did not exclude them.
Last edited by berndbausch; 11-03-2019 at 09:32 AM.
actually you can hardly implement those bash oneliners in python (in one line), and obviously there are cases where bash is more handy, but anyway the answer is no (there is no anything that bash can do and python cannot)
And it is just my own comment: bash does not work "alone" but together with [external] utilities like grep, tail, sed, whatever, and also python does not work "alone" but with a huge amount of modules (partially written in c).
that is more or less pointless. There are expressions which are simpler/shorter in bash and there are others which are simpler/shorter in python.
what do you mean by "lot"?
Are there any commands in Bash that would require a lot of programming in Python to implement?
Quote:
Originally Posted by pan64
There are expressions which are simpler/shorter in bash and there are others which are simpler/shorter in python.
I concur with pan64's perspective for that last question.
Quote:
Originally Posted by grumpyskeptic
If there is no response I will assume that there is not.
You kidding me? Likely there will always be opinions. And hence:
Quote:
Originally Posted by pan64
that is more or less pointless.
Quote:
Originally Posted by berndbausch
As far as I know, both are Turing-complete.
Kudos! I know what this means, have zero idea how to look that up and verify it, but the point there is that each is a viable language for computation.
My personal answer is that I prefer one over the other, I know one far better than the other, so that's what I use. Specifically stating which one of those is, is non-relevant to your question, I feel you should decide which language you feel more comfortable using, and concentrate on that.
Are there any commands in Bash that would require a lot of programming in Python to implement?
Probably not. There aren't many Bash commands, and I suppose they are either not very hard to implement in Python, or they only make sense in a Bash context.
As for Turing-completeness: You can implement a Turing machine in Python, thus it's Turing-complete. I suppose it's possible in Bash as well, perhaps with a little help from Bash's GNU friends. According to Wikipedia:
Quote:
an imperative language is Turing complete if it has conditional branching
Last edited by berndbausch; 11-05-2019 at 08:22 AM.
Reason: added Turing-completeness
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,804
Rep:
Quote:
Originally Posted by grumpyskeptic
Is there anything that Bash can do that Python cannot do?
I was going to suggest "be a user's shell" but that would probably get some wise guy to add it to /etc/shells and show us how it would work just fine. :^D
If you want a script to alter the parent shell, leaving you with a changed directory or with newly-set environment variables, you'd need write it in Bash and source it.
The Ranger file manager is written in Python. The component that lets you use Ranger as your cd command? Written in BASH.
This is true, but I believe it is only true because bash is the parent shell-- the one you return to when Python closes.
It's actually kind of unfair to say this is something that bash can do that Python can't, because bash can't do it either-- wait, I'll explain that (you tell me if this is not true.)
If your parent shell is the Python REPL (set in /etc/shells for example) then Python's os.chdir does the same thing that cd in bash does. If you implement a simple "command shell" with a bash script, that script wont be able to affect the CWD in the parent Python shell, only os.chdir will.
A child environment, whether bash or Python, can't change the CWD of the parent environment without a wrapper, like ranger uses. Python has the same abilities and same limitations with this-- and it isn't possible (without a wrapper) to overcome this limitation in either environment.
I do a lot with both Python and Bash, and both have their strengths. I've written scripts that are 1000s of lines and transpile to many lines of Python, but they also contain loads of bash calls without which the whole thing would be far more complicated.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.