Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Work with Environment Variables in Linux

Environment variables are a fundamental aspect of the Linux operating system, providing a way to influence the behavior of processes and applications. They are key-value pairs that can be used to store configuration settings and other information needed by the system and user applications. This article will guide you through the process of creating, modifying, and using environment variables in Linux.

What are Environment Variables?

Environment variables are dynamic values that can affect the way running processes will behave on a computer. They are used by the operating system and applications to store configuration settings and other important data.

Common Environment Variables

Some common environment variables in Linux include:

  • PATH: Specifies the directories in which executable programs are located.
  • HOME: The current user's home directory.
  • USER: The current logged-in user.
  • SHELL: The path to the current user's shell.

How to View Environment Variables

To view all environment variables, you can use the printenv or env command:

printenv

or

env

To view a specific environment variable, use the echo command:

echo $HOME

How to Set Environment Variables

Environment variables can be set temporarily or permanently.

Temporarily Setting Environment Variables

To set an environment variable temporarily, you can use the export command in the shell:

export MY_VAR="Hello World"

This variable will be available in the current shell session and any child processes spawned from it.

Permanently Setting Environment Variables

To set an environment variable permanently, you need to add it to your shell's configuration file. For example, if you are using bash, you can add the variable to the ~/.bashrc or ~/.bash_profile file:

echo 'export MY_VAR="Hello World"' >> ~/.bashrc
source ~/.bashrc

For zsh, you would add it to the ~/.zshrc file:

echo 'export MY_VAR="Hello World"' >> ~/.zshrc
source ~/.zshrc

How to Unset Environment Variables

To remove an environment variable, you can use the unset command:

unset MY_VAR

Examples

Example 1: Adding a Directory to the PATH

To add a new directory to the PATH environment variable, you can use the following command:

export PATH=$PATH:/new/directory/path

To make this change permanent, add the above line to your ~/.bashrc or ~/.zshrc file.

Example 2: Setting a JAVA_HOME Variable

If you need to set the JAVA_HOME environment variable to point to your Java installation, you can do so with the following command:

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

To make this change permanent, add the above line to your ~/.bashrc or ~/.zshrc file.

Conclusion

Environment variables are a powerful feature in Linux that allow you to configure the behavior of your system and applications. By understanding how to view, set, and unset environment variables, you can take full control of your Linux environment.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.