[Shell Tips] 在shell script 中, 將函數的結果以指定變數傳回

在shell script 中, 將函數的結果以指定變數傳回

直接看範例:

$ cat ./test.sh
#!/bin/bash
function add100 () {
  local value1=$1
  local result_var_name=$2
  eval $result_var_name=$(($value1+100))
}
sum=0
add100 30 "sum"
echo result=$sum

$ ./test.sh
result=130

此範例 add100 函數 將運算結果 放入 sum 變數中

This entry was posted in shell script and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *