我是新來的shell腳本,我想創建一個簡單的函數,它將返回連接的兩個字符串作爲參數傳遞。我試着用下面的代碼shell腳本函數返回一個字符串
function getConcatenatedString() {
echo "String1 $1"
echo "String2 $2"
str=$1/$2
echo "Concatenated String ${str}"
echo "${str}"
}
//我打電話上述功能
constr=$(getConcatenatedString "hello" "world")
echo "printing result"
echo "${constr}"
echo "exit"
我看到運行上面的代碼腳本時下面的輸出,
printing result
String1 hello
String2 world
Concatenated String hello/world
hello/world
exit
如果你看看在代碼中,我首先調用函數,然後回顯「打印結果」語句,但結果首先出現「打印結果」並在函數內部回顯語句。在下面的語句調用該函數
constr=$(getConcatenatedString "hello" "world")
或
echo ${constr}
調用函數?
因爲如果我註釋掉#echo $ {constr},那麼沒有什麼會得到迴應!請澄清我。