By default, every variable in a shell script is global, meaning it can be accessed anywhere within the script. However, a variable must be declared before it is used.
Example 1:
#! /bin/bash
f1() {
echo "x value: $x"
}
x=10
f1Output:
x value: 10
Example 2:
#! /bin/bash
f1() {
x=20
echo "x value: $x"
}
x=10
f1
echo "After f1 execution x value: $x"Output:
x value: 20
After f1 execution x value: 20
Example 3:
#! /bin/bash
f1() {
echo "x value: $x"
}
f1
x=10
f1Output:
x value:
x value: 10
Example 4:
#! /bin/bash
f1() {
x=10
}
f1
echo "x value: $x"Output:
x value: 10
To restrict a variable's scope to a function, use the local keyword. These variables can only be accessed within the function and are not available outside of it.
#! /bin/bash
f1() {
local x=10
echo "Inside function x value: $x"
}
f1
echo "Outside function x value: $x"Output:
Inside function x value: 10
Outside function x value:
Every function in shell scripting returns a value, with the default being the exit code of the last command in the function. You can explicitly return values using the return statement.
- Syntax:
return <exitcode> - Allowed values for
<exitcode>: 0 (success) to 255 (failure).
Example:
#! /bin/bash
sum() {
if [ $# != 2 ]; then
echo "You should pass exactly two numbers"
return 1
else
echo "The SUM: $(($1 + $2))"
fi
}
sum 10 20
echo "The Return value of this function: $?"
echo
sum 10
echo "The Return value of this function: $?"Output:
The SUM: 30
The Return value of this function: 0
You should pass exactly two numbers
The Return value of this function: 1
backup() {
# Commands to take backup
}
backup
if [ $? != 0 ]; then
echo "Backup failed"
else
echo "Backup successful"
fi- break: Used inside loops to terminate loop execution and continue with statements after the loop.
- exit: Terminates script execution immediately, preventing further statement execution.
- return: Used inside functions to terminate function execution. After the
returnstatement, subsequent statements in the function are skipped, but the script continues executing after the function call.