bash 星号转义在 echo 中打印 \*

bash asterisk escape prints \* in echo

我想在回显中打印 * 字符。当前行为打印我的脚本文件名:

result="multiply  *  is = $[*]"

myfile.sh 2 5 打印:

multiply 2 myfile.sh 5 is 10

我读过类似的问题和答案,我的方法是:

result="multiply  '*'  is = $[*]"

给出了:

multiply 2 '*' 5 is 10

所以另一种方法是创建一个变量:

helper="*"
result="multiply  "$helper"  is = $[*]"

但结果是一样的:

multiply 2 myfile.sh 5 is 10

有什么问题?

完整代码:

function multiply {
result="multiply  *  is = $[*]"
}

multiply  
echo $result

我尝试用 * 转义星号,但它打印出来:

multiply 2 \* 5 is 10

替换

echo $result

echo "$result"

使用shellcheck:

$ shellcheck ./myfile.sh

In ./myfile.sh line 4:
    result="multiply  *  is = $[*]"
                                  ^------^ SC2007: Use $((..)) instead of deprecated $[..]


In ./myfile.sh line 7:
multiply  
         ^-- SC2086: Double quote to prevent globbing and word splitting.
            ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
multiply "" ""


In ./myfile.sh line 8:
echo $result
     ^-----^ SC2086: Double quote to prevent globbing and word splitting.

Did you mean:
echo "$result"

For more information:
  https://www.shellcheck.net/wiki/SC2086 -- Double quote to prevent globbing ...
  https://www.shellcheck.net/wiki/SC2007 -- Use $((..)) instead of deprecated...