How to set a env variable in Linux?

Setting Environment Variables in Linux

Setting environment variables in Linux is a crucial step in customizing your system to suit your needs. Environment variables are used to store settings that can be accessed by various programs and scripts running on your system. In this article, we will explore how to set environment variables in Linux, including how to set them globally, locally, and using a shell.

Setting Environment Variables Globally

To set environment variables globally, you need to add them to the system’s ~/.bashrc or ~/.bash_profile file. Here’s how to do it:

  • Using ~/.bashrc file:

    • Open the ~/.bashrc file in a text editor.
    • Add the following line at the end of the file: export **MY_VAR**=value
    • Save the file and close it.
    • Restart the terminal or run source ~/.bashrc to apply the changes.
  • Using ~/.bash_profile file:

    • Open the ~/.bash_profile file in a text editor.
    • Add the following line at the end of the file: export **MY_VAR**=value
    • Save the file and close it.
    • Restart the terminal or run source ~/.bash_profile to apply the changes.

Setting Environment Variables Locally

To set environment variables locally, you need to add them to the system’s ~/.bashrc or ~/.bash_profile file. Here’s how to do it:

  • Using ~/.bashrc file:

    • Open the ~/.bashrc file in a text editor.
    • Add the following line at the end of the file: export **MY_VAR**=value
    • Save the file and close it.
    • Restart the terminal or run source ~/.bashrc to apply the changes.
  • Using ~/.bash_profile file:

    • Open the ~/.bash_profile file in a text editor.
    • Add the following line at the end of the file: export **MY_VAR**=value
    • Save the file and close it.
    • Restart the terminal or run source ~/.bash_profile to apply the changes.

Setting Environment Variables Using a Shell

To set environment variables using a shell, you can use the export command. Here’s how to do it:

  • Using export command:

    • Open a terminal and type export MY_VAR=value
    • Press Enter to apply the changes.
    • You can also use export MY_VAR=value to set multiple variables at once.

Common Environment Variables

Here are some common environment variables that you should be aware of:

  • PATH: This variable stores the directories where the system searches for executable files.
  • HOME: This variable stores the user’s home directory.
  • USER: This variable stores the user’s username.
  • SHELL: This variable stores the shell that the system uses to execute commands.

Tips and Tricks

Here are some tips and tricks to help you set environment variables in Linux:

  • Use export command: The export command is used to set environment variables. It is the most common way to set environment variables in Linux.
  • Use source command: The source command is used to apply environment variables that are set in a shell file.
  • Use export command with multiple variables: You can use the export command with multiple variables at once to set multiple environment variables at once.
  • Use export command with = operator: You can use the = operator to set environment variables. For example, export MY_VAR=value sets the variable MY_VAR to the value value.

Conclusion

Setting environment variables in Linux is a crucial step in customizing your system to suit your needs. By following the steps outlined in this article, you can set environment variables globally, locally, and using a shell. Remember to use the export command to set environment variables, and use the source command to apply them. With these tips and tricks, you can set environment variables in Linux with ease.

Table: Environment Variables in Linux

Variable Description Usage
PATH Stores the directories where the system searches for executable files export PATH=$PATH:/usr/bin
HOME Stores the user’s home directory export HOME=/home/user
USER Stores the user’s username export USER=JohnDoe
SHELL Stores the shell that the system uses to execute commands export SHELL=bash
MY_VAR Stores a variable value export MY_VAR=value

Example Use Cases

  • Setting environment variables for a specific user: You can set environment variables for a specific user by using the export command with the USER variable. For example: export USER=JohnDoe sets the variable USER to the value JohnDoe.
  • Setting environment variables for a specific directory: You can set environment variables for a specific directory by using the export command with the PATH variable. For example: export PATH=/usr/bin:/bin sets the variable PATH to the value /usr/bin:/bin.
  • Setting environment variables for a specific shell: You can set environment variables for a specific shell by using the export command with the SHELL variable. For example: export SHELL=bash sets the variable SHELL to the value bash.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top