Check if a string contains Asterisk (*)

voidAndAny picture voidAndAny · Jul 28, 2009 · Viewed 7.5k times · Source

I want to check if my string contain one or more asterisk.

I have tried this :

if [[ $date_alarm =~ .*\*.* ]]
then
    ...
fi

It worked when I launch directly the script, but not if this script is called during shutdown (script installed in run level 0 and 6 via update-rc.d)

Any idea, suggestion ?

Thanks

Answer

William Pursell picture William Pursell · Jul 28, 2009

Always quote strings.

To check if the string $date_alarm contains an asterisk, you can do:

if echo x"$date_alarm" | grep '*' > /dev/null; then
    ...
fi