Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way in Python to check whether an entry to os.environ is a variable or a shell function?

With the os module in Python we can easily access environment variables through the dict os.environ. However, I found out that os.environ does not just hold variables, but also globally defined shell functions (e.g. from the module software package).

Is it possible from within Python to find out whether a given entry in os.environ actually is a function and not a variable? Please note that a shell-agnostic solution is preferred, but I could settle for a Bash-specific solution as well.

like image 796
Michael Schlottke-Lakemper Avatar asked Nov 28 '13 06:11

Michael Schlottke-Lakemper


People also ask

How do I check if an environment variable is set in Python?

# Checking if an Environment Variable Exists in Python import os if 'USER' in os. environ: print('Environment variable exists! ') else: print('Environment variable does not exist. ') # Returns: # Environment variable exists!

How do I check my environ variable os?

Using os. You can use os. environ to get a complete list of all the environment variables along with their values. To access an environment variable, you can use the [] syntax. For example, environ['HOME'] gives the pathname of your home directory in the Linux environment.

How do I check if an environment variable is set?

In the command window that opens, enter echo %VARIABLE%. Replace VARIABLE with the name of the environment variable you set earlier. For example, to check if MARI_CACHE is set, enter echo %MARI_CACHE%. If the variable is set, its value is displayed in the command window.

How do I find environment variables in Python?

The os. getenv() method is used to extract the value of the environment variable key if it exists. Otherwise, the default value will be returned. Note: The os module in Python provides an interface to interact with the operating system.


2 Answers

This feature is bash-specific, so a test for an exported shell function needs to do what Bash does. Experimentation and source code show that Bash recognizes an environment variable as a shell function at startup by the presence of a () { prefix in its value — if the prefix is missing, or even slightly altered, the variable is treated as an ordinary data variable.

Therefore, the equivalent Python check would look like this:

def is_env_shell_func(name):
    return os.environ[name].startswith('() {')
like image 176
user4815162342 Avatar answered Oct 13 '22 22:10

user4815162342


One solution that I find to work (but that is ridiculously clumsy) is the following:

import subprocess

var = 'my_variable_name_i_want_to_check'
p = subprocess.Popen('declare -f ' + var, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.communicate()

if p.returncode == 0:
    print('function')
else:
    print('variable')
like image 42
Michael Schlottke-Lakemper Avatar answered Oct 13 '22 23:10

Michael Schlottke-Lakemper