Skip to content
Home » Start Exploring Environment / System Variables Now

Start Exploring Environment / System Variables Now

For those who are just starting out, environment variables are a way to store data that can be used by the operating system and other programs. In this article, we’ll dive into the world of bash environment variables and show you how to use them to improve your command-line experience.

Prerequisites

Before we get started, here are a few things you should know:

What are bash environment variables?

Bash environment variables are variables that are stored in the environment of a shell. They can be used by the shell, by other programs, and by the operating system. The variables are stored in the form of “name=value” pairs and can be accessed using the “$” symbol followed by the variable name. For example, the variable named “FOO” can be accessed by typing “$FOO”.

How to set environment variables

Setting environment variables in bash is quite simple. All you need to do is use the “export” command followed by the variable name and value. For example:

export FOO=bar

In this example, we’re setting the environment variable “FOO” to the value “bar”. To verify that the variable has been set, we can use the “echo” command:

echo $FOO

The output of this command should be “bar”.

How to unset environment variables

To unset an environment variable, you can use the “unset” command followed by the variable name. For example:

unset FOO

This will remove the environment variable “FOO” from the environment.

How to use environment variables

There are many ways to use environment variables in bash. Here are a few practical examples:

  • Setting the PATH environment variable: The PATH environment variable is used to specify the directories in which the shell should look for commands. To add a directory to the PATH, you can use the following command:
export PATH=$PATH:/path/to/directory
  • Setting the EDITOR environment variable: The EDITOR environment variable is used to specify the default text editor that should be used by certain programs. For example, if you want to set nano as your default text editor, you can use the following command:
export EDITOR=nano
  • Setting the HOME environment variable: The HOME environment variable is used to specify the home directory of the current user. To change the home directory, you can use the following command:
export HOME=/new/home/directory

5 Tips for using bash environment variables

  1. Use uppercase letters for variable names to avoid collisions with shell variables and reserved words.
  2. Avoid using spaces in variable names and values. If you need to use spaces, wrap the variable value in quotes.
  3. Use meaningful variable names to make it easier for others (and yourself) to understand what the variable is used for.
  4. Use the “export” command to make environment variables available to child processes.
  5. Use the “unset” command to remove environment variables when they are no longer needed.

Examples

Here are a few examples of system variables which you can use in bash scripts for more automation.

# Display the current date
echo "Today's date: $(date)"

# Display the username of the current user
echo "Username: $USER"

# Display the hostname of the current system
echo "Hostname: $(hostname)"

# Display the current working directory
echo "Current working directory: $(pwd)"

# Display the number of seconds since the epoch
echo "Seconds since the epoch: $(date +%s)"

# Display the value of the SHELL environment variable
echo "Shell: $SHELL"

# Display the number of currently running processes
echo "Number of running processes: $(pgrep -c -u $(whoami))"

# Display the value of the HOME environment variable
echo "Home directory: $HOME"

# Display the value of the LANG environment variable
echo "Language: $LANG"

# Display the current time as "HH:MM:SS"
echo "Current time: $(date +%T) [itvraag.nl]"

To view all environment variables in bash, you can use the env command. This command will display a list of all environment variables and their values. For example:

env

The output of this command will look something like this:

HOME=/home/user
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
LANG=en_US.UTF-8
...

Conclusion

In conclusion, bash environment variables are a powerful tool for storing data that can be used by the shell, other programs, and the operating system. By using environment variables, you can improve your command-line experience by customizing the behavior of certain programs, adding directories to the PATH, and more. To get the most out of bash environment variables, be sure to use meaningful variable names, avoid spaces, and use the “export” and “unset” commands as needed.

Next Steps

To continue your journey with bash environment variables, consider reading about the following topics:

  • The .bashrc file and how to use it to set environment variables
  • The /etc/environment file and how to use it to set system-wide environment variables

Challenge

To test your knowledge of bash environment variables, try the following challenge:

  1. Create an environment variable named “GREETING” and set its value to “Hello, World!”.
  2. Use the “echo” command to print the value of the “GREETING” environment variable.
  3. Add the “/usr/bin” directory to the PATH environment variable.
  4. Verify that the “/usr/bin” directory has been added to the PATH by using the “echo” command to print the value of the PATH environment variable.

Good luck and happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *

seventeen + seven =