Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the current shell type from python script

Tags:

python

bash

shell

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.

like image 743
Viks Avatar asked Jan 20 '14 07:01

Viks


People also ask

How do I get the current shell in python?

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.

How do I know my shell type?

Use the following Linux or Unix commands: ps -p $$ – Display your current shell name reliably. echo "$SHELL" – Print the shell for the current user but not necessarily the shell that is running at the movement.

What is shell name in python?

Python provides a Python Shell, which is used to execute a single Python command and display the result. It is also known as REPL (Read, Evaluate, Print, Loop), where it reads the command, evaluates the command, prints the result, and loop it back to read the command again.

How do I know if I am using bash or ZASH?

Now, coming to the article's main subject, how will you know that you have bash or zsh? The answer is quite simple. Use the “–version” command to confirm the existence of both shells on your Linux system.


1 Answers

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.

like image 197
DevOops Avatar answered Oct 26 '22 14:10

DevOops