14

According to the accepted answer for this SO question: , Python can make a great bash replacement.

My question then, is this: how do I go about making a seamless switch? I think the main thing to sort out to make such a switch would be: when starting a virtual terminal, call some Python shell (what though?), rather than something like Bourne shell.

Does that make sense? If yes, how could I go about doing that? This Wikipedia comparison of common shells doesn't list a single Python shell: Comparison of command shells

bzm3r
  • 363
  • 1
  • 2
  • 15

4 Answers4

18

I know this question is quite old now, but there is a new shell based on a superset of Python 3 called xonsh which might be what you are looking for.

from the website:

Xonsh is a Python-ish, BASHwards-looking shell language and command prompt. The language is a superset of Python 3.4+ with additional shell primitives that you are used to from Bash and IPython. It works on all major systems including Linux, Mac OSX, and Windows. Xonsh is meant for the daily use of experts and novices alike.

See it at xon.sh

UltraBob
  • 103
  • This should be the accepted answer. It's exactly that: Python as shell, but used as if it's Bash (i.e. running commands directly, without invoking subprocess). – CamilB Sep 04 '20 at 07:10
17

That thread and its accepted answer in particular are about using Python for shell scripting, not as an interactive shell.

To write scripts in a different language, put e.g. #!/usr/bin/env python instead of #!/bin/bash at the top of your script.

If you want to try out a different interactive shell, just run it, e.g. type ipython at your existing shell prompt. If you've decided to adopt that shell, set the SHELL environment variable at the start of your session (in ~/.profile in most environments, or in ~/.pam_environment), e.g. export SHELL=/usr/bin/ipython (.profile syntax) or SHELL="/usr/bin/ipython" (.pam_environment syntax).

None of the shells that I've seen based on advanced languages such as Perl or Python are good enough for interactive use in my opinion. They're too verbose for common tasks, especially the common job of a shell which is to launch an application. I wrote about a similar topic 4 years ago; I don't think the situation has fundamentally improved since then.

2

Rather not

The reason is that Python has no support for dealing with elevated privileges. The worst case is with editing system files.

Compare

sudo sed -i -e "/\#LXC_DOMAIN/ s/\#//" /etc/default/lxc-net

with:

out = subprocess.run('''sudo sed -i -e "/\#LXC_DOMAIN/ s/\#//" /etc/default/lxc-net''', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,env={"PATH": "/usr/bin"})

You can't use Python's native file handling for system files, because Python is inherently unable to execute subcommands with elevated privileges.

  • 1
    I'm not sure why sudo is worse than anything else. If you need sudo, you need to run /usr/bin/sudo, which is a program like any other. Your particular example here showcases the difficulty in taking shell mentality and using it from Python; a better way might be possible, but it'd be a less direct translation. – rosuav Jun 17 '19 at 04:18
1

Ipython is ok. Also, look at the 'os' library.

eyoung100
  • 6,252
  • 23
  • 53
user400344
  • 581
  • 4
  • 5
  • You're right, IPython should have been an obvious choice...but how do I get IPython to start inside a terminal emulator, or inside an actual terminal, automatically? – bzm3r Dec 27 '14 at 01:36
  • The command:chfn -s $( which ipython ) – user400344 Dec 27 '14 at 01:37
  • That came out garbled... Just: chfn -s $( which ipython ) ... Make sure the full path to ipython is in /etc/shells . – user400344 Dec 27 '14 at 01:39
  • another option is to exec ipython from the current shell startup file, like .bashrc – ptman Jan 06 '16 at 12:01