LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 11-03-2019, 08:43 AM   #1
grumpyskeptic
Member
 
Registered: Apr 2016
Posts: 472

Rep: Reputation: Disabled
What can Bash do that Python cannot?


I am learning Python, and I see that it can do things like creating .zip files and renaming files.

Is there anything that Bash can do that Python cannot do?

Thanks.
 
Old 11-03-2019, 08:53 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
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).
 
Old 11-03-2019, 09:08 AM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,243

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Here's one thing that comes to mind:

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.

Last edited by dugan; 11-03-2019 at 09:20 AM.
 
1 members found this post helpful.
Old 11-03-2019, 09:31 AM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by dugan View Post
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.
 
1 members found this post helpful.
Old 11-03-2019, 09:36 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,962

Rep: Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332
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).
 
1 members found this post helpful.
Old 11-05-2019, 02:31 AM   #6
grumpyskeptic
Member
 
Registered: Apr 2016
Posts: 472

Original Poster
Rep: Reputation: Disabled
Thanks.

Another way of asking the question is:

Are there any commands in Bash that would require a lot of programming in Python to implement?

If there is no response I will assume that there is not.
 
Old 11-05-2019, 02:45 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,962

Rep: Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332
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"?
 
1 members found this post helpful.
Old 11-05-2019, 06:42 AM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Quote:
Originally Posted by grumpyskeptic View Post
Are there any commands in Bash that would require a lot of programming in Python to implement?
Quote:
Originally Posted by pan64 View Post
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 View Post
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 View Post
that is more or less pointless.
Quote:
Originally Posted by berndbausch View Post
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.
 
Old 11-05-2019, 08:19 AM   #9
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by grumpyskeptic View Post
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
 
Old 11-05-2019, 11:09 PM   #10
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,804

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by grumpyskeptic View Post
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
 
Old 11-06-2019, 12:28 AM   #11
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by grumpyskeptic View Post
I am learning Python, and I see that it can do things like creating .zip files and renaming files.

Is there anything that Bash can do that Python cannot do?

Thanks.
yes

consume less disk space
 
Old 11-06-2019, 04:55 AM   #12
freemedia2018
Member
 
Registered: Mar 2019
Distribution: various automated remasters
Posts: 216

Rep: Reputation: 208Reputation: 208Reputation: 208
Quote:
Originally Posted by dugan View Post
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.
 
Old 11-06-2019, 06:26 AM   #13
grumpyskeptic
Member
 
Registered: Apr 2016
Posts: 472

Original Poster
Rep: Reputation: Disabled
Thanks Berndbausch for your latest reply, that answers the question for me.

Thanks to others also, but I meant in practical terms, not in the theoretical sense of Turing-completeness.

Last edited by grumpyskeptic; 11-06-2019 at 06:28 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I got error while installing python-tk python-psycopg2 python-twisted saili kadam Linux - Newbie 1 09-05-2015 03:03 AM
[SOLVED] Python TypeError when calling Python program from a Bash script noppeli Programming 2 01-15-2013 08:06 AM
[SOLVED] Python Installed; bash command: python base.py. gets syntax error in base.py cre84j Linux - Software 4 01-23-2011 04:46 PM
Python: bash-shell-like less functionality in the python shell narnie Programming 4 06-29-2010 12:25 PM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration