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.
Practical example
In this example, we create example say_hello() function and run it inside a strings assigning results to the corresponding variables.
#!/bin/bash
function say_hello() {
echo "Hello $1";
}
# Usage example:
name_1="Tom"
result_1="$(say_hello "$name_1")" # runs say_hello() and puts output into result_1 variable
name_2="Mark"
result_2="$(say_hello "$name_2")" # runs say_hello() and puts output into result_2 variable
echo "name: $string_1 result: $result_1"
echo "name: $string_2 result: $result_2"