Creating environment variables in a Linux shell is straightforward. Here’s a step-by-step guide:
1. Using the export Command
You can create an environment variable using the export command. Here’s how:
Step-by-Step Instructions
Open your terminal.
Create an environment variable: Use the following syntax:
export VARIABLE_NAME="value"For example:
export MY_VARIABLE="Hello World"Verify the variable: You can check if the variable was created successfully by echoing it:
echo $MY_VARIABLEThis should output:
Hello World
2. Creating Variables in a Script
If you want to create environment variables in a script, follow these steps:
Create a new script file:
touch my_script.shOpen the script in a text editor and add the following content:
#!/bin/bash export MY_VARIABLE="Hello from my script" echo "My variable: $MY_VARIABLE"Make the script executable:
chmod +x my_script.shRun the script:
./my_script.sh
3. Setting Variables Permanently
To make an environment variable persistent across sessions, you can add it to your shell's configuration file (like .bashrc or .bash_profile for Bash users):
Open the configuration file:
nano ~/.bashrcAdd the export command at the end of the file:
export MY_VARIABLE="Persistent Value"Save and exit the editor.
Apply the changes:
source ~/.bashrc
Summary
- Use
export VARIABLE_NAME="value"to create an environment variable. - Verify with
echo $VARIABLE_NAME. - For scripts, include the export command in the script file.
- To make variables persistent, add them to your shell's configuration file.
Feel free to ask if you have any more questions or need further clarification!
