11

My script sets some environment varrible, to be used by the Makefiles, so since every shell, bash(export to set varriable) and tcsh(setenv), I need to get the correct shell type, from where we have run the python, I have tried using $SHELL varrriable, but it always given me sh as my shell.

Is there a way, I can get the right shell type from a python script or will it always output "SH" as the $SHELL variable.

3
  • Why do you need the shell for this? subprocess.* functions have arguments for environment variables! Commented Jan 20, 2014 at 7:43
  • 2
    $SHELL gives you the login shell, not the current shell. Why not specify a specific shell in the Makefile, or use #!? Commented Jan 20, 2014 at 9:20
  • 2
    I need this, because different users can have different shell preference, and the scripts should detect from which shell, the user have invoked it, and then based on that, it should output the correct data varriables (setenv/export) for it. Commented Jan 20, 2014 at 18:52

3 Answers 3

11

Sorry to bring this back from the dead but I just did this myself. The following should give you the path to your current shell:

from os import environ
print(environ['SHELL'])

You should be able to replace shell with any environment variable you're looking for.

Sign up to request clarification or add additional context in comments.

4 Comments

doesn't work: my shell is set to fish, when I start bash in fish SHELL is still set to fish, it never changes to bash, not exec bash, not bash -c "env | grep SHELL" or anything other I can think of
your configured shell is still fish though. The environment variable approach this is using wouldn't detect that you dropped into a Bash process to exec python, that's not it's design. If you want a parent proc, I should think you can do that with subprocess somehow. What's the use case herein though? It seems like a manufactured issue.
There are plenty of reasons why you would like to know what shell called the python script. e.g. when you have written a python script that outputs shell commands for the current shell as in the question. It doesn't matter what the configured shell is when I need to generate output that is meant for the current shell that is calling python.
One could have different environment variables, aliases, etc. in .bashrc than in config.fish or .cshrc. Also you could run different script in background simple by starting a different shell. For example when using default system shell: running python use the system python (2.6 in my case) whereas changing shell running python will run the latest python (3.7) with access to non-default libraries, etc.
2

use shellingham like below

import shellingham

try:
    shell = shellingham.detect_shell()
except shellingham.ShellDetectionFailure:
    shell = provide_default()

the shell will return you a tuple with two params and you can read your shell name with shell[0]

Comments

1

For the case that a user has launched a shell of a different type from their login shell, and then invoked your Python script in that shell, you need to look at the executable used to run your script's parent process:

import os
os.path.realpath(f'/proc/{os.getppid()}/exe')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.