1

I'd like to replace bash with IPython on my system, so that even files like .profile are in Python rather than shell script.

I have some confusion on the topic, even though asking a few questions on the matter. Here are the questions I have asked previously:

1) How can I replace bash with Python?

From the answer to that question, I learned how to automatically replace bash with IPython after log in (note that it still uses .profile written in shell script).

2) .profile is written in shell script — can I instead make my system understand that I want it to execute a Python script instead?

From the answer to that question, I learned that .profile file is "sourced" rather than "executed" -- that is, the lines in .profile are run one by one, as if one were typing them into the shell.

So, my question now is: how do I get my base system shell to be IPython, so that it can source .profile files with IPython magics/Python written inside them? For example, I'd like to write my .profile file using IPython magics/Python.

bzm3r
  • 363
  • 1
  • 2
  • 15
  • 3
    iPython is not a shell, it is a Python interpreter / interactive environment. There is no reasonable way to do what you are asking. You can have your shell profile or rc file automatically start iPython for you, but that will not make it your shell; it will just be a program that your shell starts for you. – DopeGhoti Dec 10 '15 at 17:51
  • @DopeGhoti see: http://stackoverflow.com/a/209562/3486684 – bzm3r Dec 10 '15 at 17:52
  • There is the fledgeling pysh shell which can be found at https://github.com/yunabe/pysh which may do what you are looking for. – DopeGhoti Dec 10 '15 at 17:58
  • @DopeGhoti Did you also read the answer I linked to? – bzm3r Dec 10 '15 at 18:30
  • 1
    I did. It does a very good job of explaining that Python can in fact do nearly every thing a shell does, but that does not negate the fact that Python is a scripting language and not a bona fide shell. I would not be shocked to see some Pythonic flavor of csh or tcsh sometime down the line, but that day is not today. – DopeGhoti Dec 10 '15 at 18:40
  • 1
    @DopeGhoti Makes sense -- but what about IPython (rather than Python) as the "bona fide" shell, since IPython is very shell like? By the way, I am sorry if these questions are stupid, since if it's not obvious yet, I am a total newbie. – bzm3r Dec 10 '15 at 18:55

2 Answers2

3

If you are interested in using Python as a shell language and command prompt, you should look into xonsh. It is a Python based shell where you can write scripts in Python.

Krackout
  • 2,642
  • 12
  • 26
SilentGuy
  • 154
  • A great answer, can't understand why it was downvoted – Krackout Feb 08 '21 at 08:23
  • @Krackout Because it doesn't attempt to answer the question. It may be an interesting tidbit, but it's only tangentially related to the question. – Kusalananda Feb 08 '21 at 18:28
  • @Kusalananda I disagree, xonsh does exactly that; it's Python plus shell syntax, able to replace bash. – Krackout Feb 08 '21 at 18:53
  • @Krackout It may be so, but this is not an answer as it does not say anything other than "try this out". It's akin to a comment. In fact, there is already a comment suggesting pysh. This is therefore on par with that comment, in its current form. Feel free to expand on this answer. – Kusalananda Feb 08 '21 at 19:40
2

If you want to have Python launched as your default shell by the login(1) process, you can change your shell in the /etc/passwd file using the command:

usermod -s /usr/bin/python yourusername

The.profile script will still not be executed. This filename/location is hardcoded into bash(1). If you really want this, you could set up a shebang (#!) script that is interpreted by python and make this your user shell. You can then enter interactive mode from the script using code.interact().

This won't work very well though (if at all). Python's interactive mode doesn't perform most of the functions of a shell (e.g. job control) and os.system won't help, as your system shell is now Python.

  • Thanks for this answer! One thing that remains unclear to me is whether you mean "IPython" when you refer to "Python"? – bzm3r Dec 10 '15 at 19:02
  • 1
    This isn't recommended, unless you also update /etc/shells. (Assuming things have changed recently or configured specially...) if your login shell is not listed in /etc/shells, you won't be able to login. – Otheus Dec 17 '15 at 10:52