How to Define a Variable with or without Export in Bash


In Bash scripting, defining variables is an essential aspect of writing efficient and functional scripts. However, there are different ways to define variables, with and without the “export” keyword. This article will explore both methods, providing a clear understanding of when and how to use them. So, whether you’re a beginner or an experienced scripter, let’s dive into the intricacies of defining variables in Bash.

Defining a Variable without Export in Bash

When a variable is defined without using the “export” keyword, it becomes a local variable with restricted accessibility. It is limited to the current shell session or script and cannot be accessed by child processes or scripts invoked within the current script. Local variables are particularly useful for storing temporary values that are only required within specific sections of a script. For instance, consider a scenario where a script defines a variable called VAR without export.

#!/bin/bash

VAR="Hi, Linux Genie!"

echo $VAR

./file1.sh

In this scenario, the variable VAR is defined as a local variable within the current script. As a result, it can only be used inside the restrictions of the script. When the script is run, the value of VAR will successfully be used to print “Hi, Linux Genie!” to the console. However, if the script calls a child script (./file1.sh), the child script will not have access to the value of VAR. This restriction highlights the isolated nature of local variables within their respective scripts.

The image provided clearly demonstrates that when the child script is invoked, it encounters difficulty in accessing the value of the variable. As a result, it outputs an empty line instead of the expected script file output. This situation serves as a visual representation of the limited accessibility of variable values across scripts, emphasizing the significance of properly managing variable scope and communication between different script files.

Defining a Variable with Export in Bash

In contrast, when a variable is defined with the export keyword, it becomes an environment variable. Environment variables are accessible to all child processes launched from the active shell session or script, giving them a wider range of usage. This allows the variable to be utilized across various scripts or commands that are invoked from within the current script. Let’s examine the same script but with VAR defined using export:

#!/bin/bash

export VAR="Hi, Linux Genie!"

echo $VAR

./file1.sh

In this modified script, the export keyword is used to define the variable VAR as an environment variable. When the script is run, it will use the value of VAR to print “Hi, Linux Genie!” on the console. Furthermore, when the child script, ./file1.sh, is called, it can successfully access and utilize the value of VAR, resulting in the expected output.

The provided image demonstrates that when the child script is invoked, it successfully accesses the value of the variable and returns the expected output, which is “Hi, Linux Genie”. This showcases the effectiveness of using the export keyword to define a variable as an environment variable.

Note: In the provided example, a child script is created to access a variable from the main script. You must use the “chmod +x” command to make the child script executable in order to ensure it can be run.

#!/bin/bash

echo $VAR

Conclusion

A basic component of Bash scripting is the definition of variables, which enables you to store and manipulate data inside your scripts. Whether you choose to define variables with or without the “export” keyword depends on your specific requirements. Remember, defining variables without export creates local variables limited to the current session or script, while using export defines environment variables accessible to child processes and subsequent scripts.

Print Friendly, PDF & Email
Categories