If you find that one of the commands is not supported by your particular shell, then just use the other one. Both the commands should work in most command shells.These commands will set the environment variable for the current shell or instance. You can use either the set or env command to set the variable using the following syntax. If it already exist in the environment, then the value is modified to reflect the changes.
When you set a variable, the variable is created and appended to the environment if it does not already exist. Setting an environment variable is the same as creating a new value or editing an existing value. You can do this directly from the command shell as well. Often time you would want to set or modify the values as well. This section showed you how you can view the environment variables on the system. The example below will display all variables that have the word proxy in the name or the value. If you do only know part of a variable name or you want to display only a subset of the variables, then you can pipe the output of previous command to grep. This also means that it can get quite difficult to find a specific key or property value. As you already know or will find out, there are quite a long list of environment properties set by default. The output of the above commands can get pretty lengthy.
Each of these commands when executed without any command line arguments, will display all the variables and their values that are currently available in the shell environment. In case you want to see all the system variables that are set in the environment, then you can use any of the three commands : set, env or printenv. Note that the special character $ is not required by this command, as arguments to this command are assumed to be environment variable names. This works pretty much the same way as the echo command works in the previous example. If the variable name is HOME, then the command isĪnother command that can be used to display a variable value is printenv. Usually, the environment variables are all upper-case, but it need not be.
The $ is used to denote that the string following it is a variable. This works with in shell scripts as well. If you know the name of the variable that you want to lookup, then you can use the echo command to display the value of the variable. The basic concept of environment variables remain the same across shells. But it will work in other shells as well probably with slight changes in the syntax. It can also be set and unset from a single instance of the shell or session.Īlso, we will mostly deal with the bash command shell in this post. In addition to the system-wide variables, a variable can also be set into other scopes such as an user specific and session specific variables. These (pre) named objects are usually used as configuration settings that an application can lookup provided it knows the name of the variable or object. On Linux and/or Unix operating systems, an environment or system variable is a name-value pair that is available system wide and is used to share information between applications.