EN
Bash - run function and put output to string variable
0 points
In this article, we would like to show you how to run function in string and assign result to variable in Bash.
In this example, we create example say_hello()
function and run it inside a strings assigning results to the corresponding variables.
xxxxxxxxxx
1
2
3
function say_hello() {
4
echo "Hello $1";
5
}
6
7
8
# Usage example:
9
10
name_1="Tom"
11
result_1="$(say_hello "$name_1")" # runs say_hello() and puts output into result_1 variable
12
13
name_2="Mark"
14
result_2="$(say_hello "$name_2")" # runs say_hello() and puts output into result_2 variable
15
16
echo "name: $string_1 result: $result_1"
17
echo "name: $string_2 result: $result_2"