Open In App

How to Defining a Bash Variable With or Without ‘export’ in Linux

Last Updated : 07 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A variable is a basic building block that stores a value at a specific memory location, and every operation performed on a variable affects that memory location. Values ​​stored in variables can be changed or deleted during program execution. The main difference with bash variables is that they don’t need to be declared. Just assign the value of the variable using the variable name.

In Linux bash commands and scripts, there are two different ways to define variables:

  • with export command (also known as environment variable)
  • without export command (also known as shell variable).

What is Shell Variable?

Shell variables are local variables whose value we can only access within that shell. There are many uses for shell variables, some of which are listed below:

  • Shell variables are used as counter variables in Loop.
  • Shell variables are also used as temporary variables to store values.

Syntax for declaring Shell Variable:

Variable_Name = initialValue

Example:

message="Hello from GeeksforGeeks"

 

What is Environmental Variable?

Environment variables are variables whose values ​​are set globally in a program and can be inherited by subshells, processes, and commands. Environment variables have many uses, some of which are listed below:

  • It Configures the environment of the subprocess or shell
  • It defines the variables that will be used by the bash script run from the parent shell.
  • Set environment variables for terminal multiplexer such as screen or tmux.
  • It is used for Configuring the environment for building scripts and build tools

Syntax for declaring Environmental Variable:

export Variable_Name = initial value

Example:

export Message="Hello from GeeksforGeeks"

 

The main difference between the two is that the export command makes the variable available to all subsequent commands run in that shell.

Relationship Between Parent Shell and Sub Shell

Parent Shell

Sub Shell (also known as Child shell)

The parent shell can export its variables to the subshell environment. A subshell cannot export or modify its variables back to the parent shell.
The parent shell has a special ability to copy exported variables and their values ​​when the subshell is created.  The subshell keeps a copy of this environment variable.
Variables exported from the subshell are not available in the parent shell. Variables exported from a subshell are only available in the children of subsequent subshells.

More on Export Command 

Exporting Bash Functions

The Export command can also be used to export bash functions.

Step 1: Create a Bash Function

function message{
echo "Hello from GeeksforGeeks"
}

 

Step 2: We can use the export -f command line option to export functions so that they are also available in subshells and processes:

export -f message

The function is now available in sub-shells.

 

Removing the value of an Exported Variable

For removing the value of an exported variable we can use the unset command which is in-built bash command that can be used to delete the value of an exported variable.

Syntax

unset Environmental_Variable_Name 

Example

unset message

 

Viewing All Exported Variables

Step 1: If the export command takes no arguments, it displays a list of all variables. To export all listed variables to the subprocess, use the -p option.

export -p

 

Step 2: With the help of the below command, we can undo the effects of exporting -p command.

export -n

The variable is again restricted to the current shell session.

More on Bash Script and Export

In order to get the expected results with the export command when creating a bash script, we need to keep the following points in mind:

Export when running a bash script

Suppose you run a bash script in its own subshell inside a bash command that contains an export command. So the variables exported from the script are only used in the subshell, not the parent shell.

Step 1: You can easily create a file to store your script (“outscript.sh”) with the following command:

echo "export message='Hello from GeeksForGeeks'" > outscript.sh

 

Step 2: With the help of “chmod +x”, we can load “outscript.sh” into bash and run it.

chmod +x outscript.sh

 Exported variables disappear from the environment.

 

Source Variables From a Bash Script to the Current Shell

As shown above, we know that the bash script will never export its exported variables back to the calling shell, but the bash script also provides us with a command that can handle this situation easily. The source command allows us to load variables and functions into the bash shell and use them in the current shell without creating a subshell.

Step 1: You can easily create a file to store your script (“outscript.sh”) with the following command:

echo "export message='Hello from GeeksForGeeks'" > outscript.sh

Step 2: With the help of “source”, we can load “outscript.sh” into bash and run it.

source outscript.h

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads