9

I recently got a book on shell scripting in bash. It states to list all the Environment Variables using the printenv command. I've noticed though that this doesn't list all variables, for example $PWD or $REPLY or $SSH_TTY. Is their a complete list I can reference for all of these Environment Variables and their Functions?

7
  • 2
    $REPLY is a builtin variable from the shell not environment. for your question try declare -x Commented May 3, 2020 at 19:36
  • 2
    The set command might do what you want. Commented May 3, 2020 at 19:39
  • What is the difference between the an Environment Variable and a shell built-in variable? They are both set by the shell not the user? Commented May 3, 2020 at 19:51
  • Apropos the initial question, set and declare -x seem to be working for me. Thank You Commented May 3, 2020 at 19:56
  • printenv will show all the environment variables that are set in your current shell. It doesn't show all that could be set (you can technically set almost any string as an environment variable). There are a large number that will be set under certain circumstances (e.g. in an ssh session); or that, if set, would have a special function (e.g. the LANG and LC_* variables, which would tell programs what locale to use; see man locale). There are no full lists of those, because anyone who writes a program can make it create/react to any variable they want, thus creating a new one. Commented May 3, 2020 at 20:28

2 Answers 2

12

To display a list of the Environment Variables you can use

printenv # or set to show also shell environment vars thanks @gordon set #if you want to see it nicely you can pipe it to more like this printenv | more set | more 
Sign up to request clarification or add additional context in comments.

3 Comments

set will show both shell and environment variables, without distinguishing which are which.
@OdedBD : The OP explicitly asked for environment variables. Asides from this, set is certainly useful, and we should point out that it additionally lists the functions as well.
You could also using declare -p to list all shell variable.
5

Within a shell like bash there are two types of variables; environment variables (Wikipedia) and shell variables. There are a number of predefined shell variables.

You can use the export built-in to "promote" a shell variable to an environment variable, which has the effect of making that variable available to any subprocesses launched from the shell.

As the name implies, printenv only reports the process' environment variables. Variables like PWD or REPLY are shell variables, and thus aren't displayed. As suggested in the comments, invoking set with no arguments will print all variables (environment and shell) available in your current session.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.