1

I'm looking for some way to print an asterisk in a string passed to a program in shell. These problems are similar but I haven't been able to make any of the solutions work in my case: how do I echo an asterisks to an output, Printing asterisk (*) in bash shell, How do I escape the wildcard/asterisk character in bash?.

I have the following script:

#!/bin/bash

# First function
function()
{
typeset -r str=$1
typeset -i N=$2
typeset -i i=0
while [[ $i -lt $N ]];
do
    echo $str| sed "s/<token>/$i/g"
    (( i+=1 ))
done
}

# 'main' function
doStuff()
{
foo.pl << EOF

some words $(function "input string with asterisk * and some <token> after it" 123)
some more words

EOF
    [ $? -eq 0 ] || logerror "Function 'doStuff' failed."
}

doStuff

exit 0

When running the skript, the asterisk is substituted with the results of echo *. To fix this I have tried declaring ASTERISK='*' and substituting it in, as well as simply altering the function call to $(function "input string with asterisk "'*'" and some <token> after it" 123), but neither did the trick.

I suspect the problem is the echo statement inside function, but am not quite sure how to solve it, so my question would be if there is any way to cast str in function to not allow for interpolation once it is within function?

abcalphabet
  • 1,158
  • 3
  • 16
  • 31

2 Answers2

1

As per arco444's suggestion, using set -o noglob before the sed in function did the trick.

abcalphabet
  • 1,158
  • 3
  • 16
  • 31
0

You can surround the variable expansion with "" to avoid the glob.

echo "$str" | ...
ccarton
  • 3,556
  • 16
  • 17