Linux – How to enter star * inside a string in shell

bashlinuxshellUbuntu

How can I include * inside a string?

Here is my code:

#!/bin/bash
# This is a simple calculator using select statement
echo -n 'Insert 1st operand: '
read first
echo -n 'Insert 2nd operand: '
read second
echo 'Select an operator:'
operators="+ - * /"
select op in $operators
do let "result=${first}${op}${second}"
   break
done
echo -e "Result = $result"

When I run this code, * will list all files in current directory as select choices. I tried to escape it with \* but it doesn't work.

Best Answer

The shall expands its parameters. But then select expands its parameters too. The shell expands \* to just *, which doesn't help, since select then expands that *. You need something that expands to \*, which would be \\*.

Alternatively, just use:
select op in + - \* /;
or:
select op in "$operators"