Escaping backslash and asterisk in egrep to match "\*"

So far what i've got is

egrep '^(\\)\*$'

No luck.

I've searched the web and not much luck. I know about the escape character

\

but its confusing to figure out how to use it to match a backslash and use it to escape the asterisk also. Any ides? Thanks!

Without knowing what you're trying to match, what your input looks like, and what output you're trying to get, there isn't much we can do to help.

What you have appears to work fine for me:

$ cat testit
\*
$ od -c testit
0000000   \   *  \n
0000003
$ egrep '^(\\)\*$' testit
\*

IM trying to match "\" as a parameter when running a script. The input is "\". I have the if statement already i just need the regular expression to not return a empty string.

elif [ "$(echo $domain | egrep '^(\\)\*$')" != "" ]; then

---------- Post updated at 05:38 PM ---------- Previous update was at 05:37 PM ----------

THats what i thought but when i run my script and parameter as "/*" i don't get he output i want. THe output i want is this echo in this if statement.

elif [ "$(echo $domain | egrep '^(\\)\*$')" != "" ]; then
        echo $domain is a * wilcard

What you want is:

elif (echo $domain | egrep -q '^(\\)\*$') ; then
        echo $domain is a * wilcard

Are you looking for /* or for \* ?

How was domain set? The results are very different if domain is set by:

domain=\*

as opposed to:

domain="\*"

What is the output from the command:

echo "$domain"|od -c

when the egrep is not giving you the results you expected.

sorry the /* is wrong it should be \* and its "\*" a string. I will run the command and check it out (the od -c command)

---------- Post updated at 12:25 PM ---------- Previous update was at 12:04 AM ----------

So i found out the output of

echo "$domain"|od -c

is

0000000   *  \n
0000002

which is right because i am trying to pass an asterisks as the first parameter when i call my script. But when the script runs , checks the parameter and get to the asterisk part of my if statement, it does not go through. Here is the part of the if statement that check for the asterisks

elif [ "$(echo $domain | egrep '^\\*$')" != "" ]; then
        checkItem

I know the whole if statement whole because for every other condition i get the right output. BUt when i enter a asterisks as a parameter, checkItem function does not get called. I have a feeling its the regular expression.

OK. So domain contains the single character asterisk ( * ), but your code is looking for a backslash and an asterisk ( \* ).
So, the simple way to get what you want is:

if [ "$domain" = '*' ]
then    checkItem
fi

If you want to make it less efficient and use grep or egrep, that would be:

if [ "$(echo "$domain" | grep '^[*]$')" != "" ]
then    checkItem
fi

Ok Don. I used the '*' way and it works now. Thanks you very much