0

The env command can be used to print environment variables in bash. I want to ask if there is any built-in command to do this ?

$ export temp="hello world"
$ env | grep temp

The second command will indeed print temp's value, but that's because it is inherited as environment when env was spawned. I want to check what environment variables the bash shell process had originally, in which temp would still not be part of environment , but just marked to be exported as environment for any child (I want to test this).

3
  • Have you tried set ?
    – steve
    Aug 13, 2015 at 7:37
  • try set -o posix; set
    – gwillie
    Aug 13, 2015 at 7:40
  • 1
    @gwillie: It will show the exported variables.
    – cuonglm
    Aug 13, 2015 at 9:12

1 Answer 1

1

You are right. When exporting or declaring a variable inside a shell, the variable is not added to the shell's environment (in the mean of updating the file - read below).

You can view the environment variable the shell (or any other process) had when it was invoked on Linux by viewing the file /proc/PID/environ, where PID is the PID of the process you want to analyze. The variables are saved in the classic form var=value and are separated by the null character.

3
  • I have two shell scripts cmdline and environ, which just do tr '\0' '\n' <<</proc/${pid}/cmdline (figure out the second one).
    – ott--
    Aug 13, 2015 at 20:01
  • Sorry, one < only instead of <<<.
    – ott--
    Aug 13, 2015 at 20:12
  • $$ has the shell's pid, so, tr '\0' '\n' < /proc/$$/environ
    – balki
    Jun 9, 2022 at 13:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .