How to create global variables in Linux
Global variables can be created in Linux by following these steps:
- Open a terminal window and input the following command to create a new global variable and assign a value to it:
export VAR_NAME=value
The variable VAR_NAME represents the name of the variable, while value represents the value of the variable. For example, to create a global variable named MY_VAR and set it to “hello”:
export MY_VAR=hello
- To verify if a global variable has been successfully created, you can use the following command to display the value of the variable:
echo $VAR_NAME
For example, display the value of MY_VAR:
echo $MY_VAR
- To automatically set global variables when the system starts, you can add the export command to the bash configuration file. Open a terminal window and enter the following command to open the bash configuration file:
vi ~/.bashrc
Add the following lines to the opened file to set global variables:
export VAR_NAME=value
Save and exit the file.
- Finally, enter the following command to apply the changes:
source ~/.bashrc
This will reload the bash configuration file and import new global variables.
Now you have successfully created a global variable that can be used anywhere in the system.