How to Set and Use Environment Variables | Bash
Environment variables are like special settings that store information about your system and user environment. They are accessible to running processes and can be used to configure their behavior. Think of them as key-value pairs, where the key is the variable name and the value is the information it holds.
Examples of common environment variables:
- HOME: Your home directory path.
- PATH: List of directories where the system searches for executable programs.
- USER: Your username.
- TERM: Your terminal type.
- EDITOR: Your preferred text editor.
These variables are set automatically by the system or your shell (like Bash) during login or when you run certain commands. You can also customize them to suit your needs.
How environment variables work in Bash
- Setting: You can define new environment variables or modify existing ones using commands like export or declare -x.
- Accessing: To use a variable, simply prepend its name with a dollar sign ($) in your commands or scripts.
- Inheritance: Child processes spawned from your shell or script inherit all the environment variables available at the time of creation.
- Scope: Environment variables are global within their scope, meaning they are accessible throughout your current shell session and any child processes.
Setting Environment Variables
Setting Temporary Environment Variables
You can set a temporary environment variable within a script using the export command. This variable will only be available for the duration of the script's execution.
After executing this script, the MY_VARIABLE environment variable will be available in the script and any child processes it spawns.
Setting Environment Variables for Subsequent Commands
If you want to set an environment variable and use it immediately in the same command or line, you can do it like this:
Here, some_command_using_variable will have access to the value of MY_VARIABLE.
Setting Environment Variables Permanently
You can also set environment variables permanently in your system by adding the export statement to your shell profile file (e.g., .bashrc, .bash_profile).
After modifying the profile file, restart your shell or run source ~/.bashrc to apply the changes.
Using Environment Variables
Accessing Environment Variables
To access the value of an environment variable, use the $ symbol followed by the variable name.
This will print the value of MY_VARIABLE to the console.
Passing Environment Variables to Commands
You can pass environment variables to commands within your script.
Default Values for Environment Variables
You can use a default value if the environment variable is not set.
If MY_VARIABLE is not set, it will default to "Default Value".
Example 1: Basic UsageInside another_script.sh:
Common Use Cases
- Storing configurations: Save configuration settings for scripts or applications (e.g., database connection details, file paths, user preferences).
- Passing information between scripts: Share data between different parts of your application or scripts.
- Customizing script behavior: Allow users to tailor script behavior by setting environment variables.
Common Environmental and Shell Variables
These variables play a crucial role in configuring the behavior of the system and user environments. Here is a list of some common environmental and shell variables:
PATH:
Description: Specifies directories where the system looks for executable files.Example: /usr/local/bin:/usr/bin:/bin
HOME:
Description: Points to the current user's home directory.Example: /home/username
USER:
Description: Represents the username of the current user.Example: username
LOGNAME:
Description: Indicates the login name of the user.Example: username
SHELL:
Description: Specifies the default shell for the user.Example: /bin/bash
PWD:
Description: Stores the present working directory.Example: /home/username/documents
PS1:
Description: Defines the format of the command prompt.Example: \u@\h:\w\$
PS2:
Description: Defines the secondary prompt for multiline commands.Example: >
LANG:
Description: Determines the default language and localization settings.Example: en_US.UTF-8
TERM:
Description: Specifies the type of terminal being used.Example: xterm-256color
DISPLAY:
Description: Defines the X Window System display server.Example: :0
LD_LIBRARY_PATH:
Description: Lists directories where the system searches for shared libraries.Example: /usr/local/lib:/usr/lib
EDITOR:
Description: Specifies the default text editor for the user.Example: vim or nano
TZ:
Description: Sets the time zone for the system.Example: America/New_York
UID:
Description: Represents the user ID (numeric) of the current user.Example: 1000
GROUPS:
Description: Lists the groups to which the user belongs.Example: users wheel
HISTSIZE:
Description: Sets the maximum number of commands stored in the command history.Example: 1000
MAIL:
Description: Specifies the location of the user's mailbox.Example: /var/mail/username
TMPDIR:
Description: Defines the directory for temporary files.Example: /tmp
HOSTNAME:
Description: Indicates the name of the host computer.Example: mycomputer
Conclusion
Environment variables in Bash are key-value pairs that store information about the system environment. They are used to configure and control the behavior of scripts and programs, with common examples including variables like PATH, HOME, and USER, influencing aspects such as executable file paths, user directories, and system localization settings. Bash scripts can set, access, and pass these variables, providing a flexible way to customize the runtime environment and share information between different parts of a script or between scripts.