Bash Wiki
Posts (Latest 30 updated) :
Read all
Contents:
  1. [Linux] Bash local command: Define local variables in a function
    1. Overview
    2. Usage
    3. Common Options
    4. Common Examples
      1. Example 1: Basic local variable
      2. Example 2: Local variable with arithmetic
      3. Example 3: Preventing variable conflicts
    5. Tips

[Linux] Bash local command: Define local variables in a function

Overview

The local command in Bash is used to create variables that are local to a function. This means that the variable will only exist within the scope of that function and will not affect or be accessible from outside it. This is particularly useful for avoiding variable name conflicts and managing memory efficiently.

Usage

The basic syntax of the local command is as follows:

local [options] variable_name=value

Common Options

  • There are no specific options for the local command itself, but it is typically used within functions to define local variables.

Common Examples

Example 1: Basic local variable

my_function() {
    local my_var="Hello"
    echo $my_var
}

my_function  # Outputs: Hello
echo $my_var  # Outputs nothing, as my_var is local to my_function

Example 2: Local variable with arithmetic

calculate() {
    local result=$(( $1 + $2 ))
    echo $result
}

calculate 5 10  # Outputs: 15

Example 3: Preventing variable conflicts

global_var="I am global"

my_function() {
    local global_var="I am local"
    echo $global_var
}

my_function  # Outputs: I am local
echo $global_var  # Outputs: I am global

Tips

  • Always use local when defining variables inside functions to prevent unintended side effects on global variables.
  • Remember that local variables are destroyed once the function exits, helping to free up memory.
  • Use descriptive names for local variables to avoid confusion, especially in larger scripts.