📜  linux set env Permanent - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:43:55.564000             🧑  作者: Mango

Linux Set Env Permanent - Shell/Bash

Introduction

Setting environment variables is a common task when working with Linux. Environment variables are key-value pairs that hold information about the environment in which a process is running. In this tutorial, we will learn how to set environment variables permanently in the Shell/Bash environment.

Setting Environment Variables

To set an environment variable in Shell/Bash, you can use the export command followed by the variable name and value. For example, to set the MY_VAR variable to my_value, use the following command:

export MY_VAR=my_value

This will set the MY_VAR variable to my_value for the current Shell/Bash session.

Permanent Environment Variables

To make the environment variable permanent, you need to add the export command to your Shell/Bash configuration file. There are two main configuration files you can use, depending on your system and Shell/Bash version:

  • ~/.bashrc: This configuration file is used by Bash when it starts as an interactive shell.
  • ~/.bash_profile: This configuration file is used by Bash when it starts as a login shell.

To add the MY_VAR environment variable to the ~/.bashrc file, open the file in an editor:

nano ~/.bashrc

Add the following line at the end of the file:

export MY_VAR=my_value

Save and close the file. The next time you start a new Shell/Bash session, the MY_VAR environment variable will be set to my_value.

Conclusion

Setting environment variables in Linux is a simple and useful task. By using the export command, we can easily set environment variables in Shell/Bash. To make the environment variables permanent, we need to add the export command to a Shell/Bash configuration file, such as ~/.bashrc or ~/.bash_profile.