0

A shell (for example: the bash shell) have the concept of shell variables and the concept of environment variables.

But what about other processes that are not shell processes (for example: Firefox, gedit, etc.), do these processes also have shell variables or do they only have environment variables?

2

A non-shell process does not have shell variables. A C program has C variables, an awk program has awk variables, a Perl program has Perl variables, etc., and these are all in their own ways very much different from shell variables.

The reason another process do not have shell variables (or gets to access shell variables) is that shell variables are not exported (i.e. they are not environment variables), and also because some shells allows for attaching more information to a shell variable than just a string value, such as attributes for read-only variables, integer-only variables, etc. These type attributes (see the typeset built-in command) can't be applied to an environment variable and be used in another process.

Some shells (like bash) also supports arrays and associative arrays. These structures are too complex for the simple key-value pair format, where both the key (the variable name) and the value are plain text strings, imposed on environment variables, which means that these can't be exported for use in a generic other process.

All processes have access to the environment variables inherited from their parent processes. Depending on language, there are different ways for a program to access these.

A C program may use getenv(), an awk program may use its associative array ENVIRON, and a Perl program may use its %ENV hash to access environment variables, for example.

I don't know gedit, but in vim, you may access environment variables with a shell-like syntax:

:echo $HOME

for example. The echo here has nothing to do with echo in the shell, it just happens to work in a similar way. Also, the $HOME string just happens to be the way that vim exposes environment variables to the user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.