Bash Export Variable


Bash is one of most commonly used shells in Unix-based operating systems. It is a command-line interface that allows users to interact with system and execute various commands. Bash is an essential tool for system administrators, developers, and programmers.

One of features of Bash is ability to export variables. In this article, we will discuss basics of exporting variables in Bash, how it works, and examples of how to use it.

What is a Bash Export Variable?

A Bash export variable is a variable that is made available to all child processes of current shell. When a variable is exported, it becomes an environment variable, which means it can be accessed by any program or script that runs in current shell environment.

How to Export a Variable in Bash

Exporting a variable in Bash is straightforward. To export a variable, you need to use export command followed by name of variable you want to export. Here is an example −

export MY_VARIABLE="Hello World"

In this example, we are exporting a variable called MY_VARIABLE with a value of "Hello World." Once this variable is exported, it becomes an environment variable and can be accessed by any child process of current shell.

You can also export multiple variables in one command. Here is an example −

export VAR1="value1" VAR2="value2" VAR3="value3"

In this example, we are exporting three variables: VAR1, VAR2, and VAR3, with their respective values.

Viewing Exported Variables in Bash

To view all exported variables in Bash, you can use env command. env command displays a list of all environment variables in current shell environment.

env

This command will output a list of all environment variables, including variables that you have exported.

Using an Exported Variable in a Script

Exporting a variable in Bash allows you to use it in any script or program that runs in current shell environment. Here is an example of how to use an exported variable in a script −

#!/bin/bash

export MY_VARIABLE="Hello World"

echo "The value of MY_VARIABLE is: $MY_VARIABLE"

In this example, we are exporting variable MY_VARIABLE with a value of "Hello World" and using it in a script. script will display value of MY_VARIABLE when it is executed.

Exporting Variables in Bash Profile

You can also export variables in Bash profile, which is a script that is executed every time a user logs in to system. Exporting variables in Bash profile ensures that variables are available every time you log in.

To export a variable in Bash profile, you need to add export command to file. Here is an example −

export MY_VARIABLE="Hello World"

Once you have added export command to Bash profile, variable will be exported every time you log in to system.

Using Exported Variables in Shell Scripts

Exported variables can be used in shell scripts to store values that can be accessed by other scripts or programs. Let's say you have a script that performs a backup operation, and you want to store backup directory path in a variable. You can export variable so that it can be accessed by other scripts or programs.

Here's an example −

#!/bin/bash

export BACKUP_DIR=/path/to/backup/directory

# Perform backup operation using $BACKUP_DIR variable

In this example, we have exported BACKUP_DIR variable with path to backup directory. We can then use this variable in our backup script or any other script that needs to access backup directory.

Exporting Variables for Remote Sessions

When you connect to a remote server using SSH, environment variables of your local system are not available on remote system. However, you can export variables on your local system and make them available on remote system using SSH command.

Here's an example −

export MY_VARIABLE="Hello World"
ssh user@remotehost "echo \$MY_VARIABLE"

In this example, we have exported MY_VARIABLE variable on our local system and used SSH command to connect to remote system. We have then used echo command to display value of MY_VARIABLE variable on remote system.

Exporting Variables in Bash Functions

You can also export variables within Bash functions. When you export a variable within a function, it becomes an environment variable and can be accessed by any child processes of current shell.

Here's an example −

#!/bin/bash

my_function() {
   export MY_VARIABLE="Hello World"
}

my_function

# value of MY_VARIABLE can be accessed by any child processes

In this example, we have defined a function called my_function that exports MY_VARIABLE variable. We have then called function, and MY_VARIABLE variable is now available to any child processes of current shell.

Unexporting Variables in Bash

You can unexport a variable in Bash using unset command. When you unset a variable, it is removed from list of environment variables and is no longer available to child processes.

Here's an example −

export MY_VARIABLE="Hello World"
echo "MY_VARIABLE is: $MY_VARIABLE"

unset MY_VARIABLE
echo "MY_VARIABLE is now unset"

In this example, we have exported MY_VARIABLE variable and used echo command to display its value. We have then used unset command to remove MY_VARIABLE variable from list of environment variables. When we try to access MY_VARIABLE variable again, it is no longer available.

Exporting Variables with Command Substitution

You can also export variables using command substitution. Command substitution is a shell feature that allows you to run a command and use its output as part of another command.

Here's an example −

export DATE=$(date +%Y-%m-%d)
echo "Today's date is: $DATE"

In this example, we have used date command with +%Y-%m-%d format specifier to get today's date. We have then exported output of date command to DATE variable using command substitution. We can then use DATE variable in any script or program that runs in current shell environment.

Exporting Variables with Quotes

When you export variables in Bash, it's important to use quotes around variable value. Using quotes ensures that value is interpreted as a single string and prevents issues with spaces or other special characters.

Here's an example −

export MY_VARIABLE="Hello, world!"

In this example, we have used quotes around value of MY_VARIABLE variable to ensure that comma and space are interpreted as part of value.

Conclusion

Exporting variables in Bash is a simple and powerful feature that allows you to make variables available to all child processes of current shell. It is a useful tool for system administrators, developers, and programmers. In this article, we have discussed basics of exporting variables in Bash, how to export variables, how to view exported variables, how to use an exported variable in a script, and how to export variables in Bash profile.

Updated on: 31-Mar-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements