EN
Bash - how to pass arguments to function
0 points
This article will show you how to pass arguments to function in Bash.
Quick solution:
xxxxxxxxxx
1
2
3
my_function(){
4
# here we can refer to subsequent arguments using $1, $2 and $3
5
}
6
7
# number of argument
8
# 1 2 3
9
my_function "one" "two" "three"
10
xxxxxxxxxx
1
2
3
print_parameter(){
4
echo $1
5
}
6
7
print_parameter "Dirask"
8
print_parameter "wiki for code"
Output:
xxxxxxxxxx
1
Dirask
2
wiki for code
xxxxxxxxxx
1
2
3
add_numbers(){
4
echo $(($1 + $2))
5
}
6
7
add_numbers 1 2
8
add_numbers 10 13
Output:
xxxxxxxxxx
1
3
2
23