0

I want to print * character in echo. Current behaviour prints my script filename:

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

myfile.sh 2 5 prints:

multiply 2 myfile.sh 5 is 10

I've read similar problem and answers and my approaches were:

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

which gave:

multiply 2 '*' 5 is 10

So another approach was to make a variable:

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

but the result was the same:

multiply 2 myfile.sh 5 is 10

What is the problem?

Full code:

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

multiply $1 $2
echo $result

I tried escaping the asterisk with * but it prints:

multiply 2 \* 5 is 10
miken32
  • 42,008
  • 16
  • 111
  • 154
MaKiPL
  • 1,200
  • 8
  • 15

1 Answers1

2

Replace

echo $result

with

echo "$result"

Use shellcheck:

$ shellcheck ./myfile.sh

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


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

Did you mean:
multiply "$1" "$2"


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...
Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38