头图

foreword

Environment variables control the look, behavior, and feel of your Linux work environment. There are two types of variables:

  • Environment Variables : These are process-wide variables built into the system that control the appearance and behavior of the system. Because they are process scoped, they are inherited by any child shell or process.
  • shell variables : are usually listed in lowercase and are only valid if they are set shell .

View and modify environment variables

Use the env command to view all default environment variables:

image.png

Environment variables are always uppercase, as we can see SHELL , WINDOWID and COLORTERM .

To view all environment variables, including shell variables, local variables, and shell functions: such as any user-defined variables and command aliases, we can use set command. I would use it with more so it looks more manageable

 set | more

image.png

change variable value

We can change a variable by providing the variable name followed by the equality sign and the new value:

 HISTSIZE=200

image.png

We changed the value of the variable HISTSIZE but only in our specific environment. If we want to make this change persistent, we can use the export command. export will set the new value from your current environment to any new fork child process, allowing the new process to inherit the exported variable.

 export HISTSIZE

Note: Before making any changes, you may want to save a copy of the current value echo $VARIABLE> ~/valueOfVARIABLE.txt , or you may even want to keep a copy of all current variable values set> ~/valueofALLon02252020.txt .

Another variable you may need to modify is the PS1 variable. It has a set of placeholders for the information you want displayed in the shell prompt:

  • \u the name of the current user
  • \h hostname
  • \w The base name of the current working directory

image.png

PATH variable

The PATH variable controls shell where to look for commands on the system. If bash shell the command is not found in a directory in your PATH variable, a command not found error will be returned.

Let's see which directory stores environment variables:

 echo $PATH

image.png

This is my terminal to search all directories for any command. Each directory is separated by a colon (:).

Say I have some command that exists in the /home/azureuser/gpstools directory and I want to add that directory to the PATH. We can add a directory to the PATH variable by typing:

 PATH=$PATH:/directory/to/add

This will add the /directory/to/add/ directory to the original PATH.

image.png

Please note: do not do PATH=/directory/to/add , that will replace all existing directories and add new ones, you want to append, not replace.

Create user-defined variables

We can create our own variables with the following syntax:

 MYVARIABLE="VALUE"

If you need to make it persistent, don't forget to export it: export MYVARIABLE . If you want to delete any variable, use the unset command: unset MYVARIABLE , which just double-checks before deleting anything.


chuck
303 声望41 粉丝