Shell Scripting Shell Variables in Linux (original) (raw)

Last Updated : 9 Mar, 2026

In Linux and Unix systems, the shell acts as a command-line interpreter that provides a bridge between the user and the system kernel. Shell variables are used in Linux shell scripting to store data that can be referenced, modified, and reused throughout a script. They make scripts more flexible and allow automation of repetitive tasks.

Types of Variables in Shell Scripting

Shell scripting supports different types of variables based on their scope, behavior, and purpose. Understanding these types helps in controlling data visibility, sharing values across scripts, and accessing system-level information.

Local Variable

A local variable is a variable whose scope is limited to a specific function or block of code. It can only be accessed within the function where it is defined. Once the function execution completes, the variable is no longer accessible.

**Example: Local Variable Scope

**Script:

#!/bin/sh

calculateSum(){
sum=50 # local variable
echo "Sum inside function: $sum"
}

echo "Sum outside function: $sum" # will not display anything
calculateSum

local_ex-scr

**Output:

local_ex-scr-op

Global Variables

A global variable is a variable that is accessible throughout the script, including inside functions. It is defined outside any function, so all parts of the script can use it. Global variables are useful when you need to share data between multiple functions within a script.

**Example 1: Global variable in the same script

**Script: global_example.sh

#!/bin/sh

Global variable

total=100

Function accessing global variable

showTotal() {
echo "Total inside function: $total"
}

Accessing global variable outside function

echo "Total outside function: $total"

Call the function

showTotal

globel_example-scr

**Output:

globel_example-scr-op

Exporting Global Variables to Child Scripts

By default, global variables are only available within the current shell script. If you want a global variable to be accessible in another script executed from the current script, you need to use export.

**Example 2: Exporting global variable to a child script

If you want a child script to access this global variable, you need to export it.

**Parent script: parent.sh

#!/bin/sh

Global variable

total=100

Export to make it available to child script

export total

Run child script

./child.sh

globle-parent-scr

**Child script: child.sh

#!/bin/sh

Access the exported variable

echo "Total in child script: $total"

globle-child-scr

**Note:

**Output:

globle-parent-scr-op

**Note:

Shell (System) Variables

Shell variables, also known as system variables, are special variables created and maintained by the shell itself. They store system-related information that the shell and scripts use to function correctly. These variables are usually defined in uppercase and are available automatically when the shell starts.

**Example: Using Shell Variables

#!/bin/bash

Accessing shell variables

echo "Bash version: $BASH_VERSION"
echo "Home directory: $HOME"
echo "Host name: $HOSTNAME"
echo "User name: $USERNAME"

shell_variable_script

**Output:

shell_variable_script-op