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:

  1. HOME: Your home directory path.
  2. PATH: List of directories where the system searches for executable programs.
  3. USER: Your username.
  4. TERM: Your terminal type.
  5. 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

  1. Setting: You can define new environment variables or modify existing ones using commands like export or declare -x.
  2. Accessing: To use a variable, simply prepend its name with a dollar sign ($) in your commands or scripts.
  3. Inheritance: Child processes spawned from your shell or script inherit all the environment variables available at the time of creation.
  4. 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.

#!/bin/bash export MY_VARIABLE="Hello, World!"

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:

#!/bin/bash MY_VARIABLE="Hello, World!" some_command_using_variable

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).

# Add the following line to your .bashrc or .bash_profile export MY_VARIABLE="Hello, World!"

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.

#!/bin/bash echo $MY_VARIABLE

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.

#!/bin/bash MY_VARIABLE="Hello, World!" echo "Using variable: $MY_VARIABLE" another_command_using_variable

Default Values for Environment Variables

You can use a default value if the environment variable is not set.

#!/bin/bash MY_VARIABLE=${MY_VARIABLE:-"Default Value"} echo "Using variable: $MY_VARIABLE"

If MY_VARIABLE is not set, it will default to "Default Value".

Example 1: Basic Usage
#!/bin/bash export GREETING="Hello" echo "$GREETING, World!"
Example 2: Passing Environment Variable to Another Script
#!/bin/bash export MY_NAME="John" ./another_script.sh

Inside another_script.sh:

#!/bin/bash echo "My name is $MY_NAME"
Example 3: Using Default Values
#!/bin/bash MY_VARIABLE=${MY_VARIABLE:-"Default Value"} echo "Using variable: $MY_VARIABLE"

Common Use Cases

  1. Storing configurations: Save configuration settings for scripts or applications (e.g., database connection details, file paths, user preferences).
  2. Passing information between scripts: Share data between different parts of your application or scripts.
  3. 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.