How can you create a variable with local scope in a shell script?

QuestionsQuestions4 SkillsProShell FunctionsSep, 30 2025
0125

In a shell script, you can create a variable with local scope by using the local keyword within a function. Here's an example:

#!/bin/bash

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

my_function
echo "$local_var"  # This will not print anything because local_var is not accessible here

In this example, local_var is defined as a local variable inside my_function, and it cannot be accessed outside of that function.

0 Comments

no data
Be the first to share your comment!