Search code examples
stringbashcontains

Bash: String contains hyphen


I am trying to see if string1 contains another string2. I do this in this manner:

a=$(tempfile)
echo "eafg" > $a

if [[ $a == *e* ]]
then
   echo "contains"
fi

Now I try to see if a string contains a hyphen:

a=$(tempfile)    
echo "22:00:00-02:00" > $a

if [ $a == *-* ]
then
   echo "contains"
fi

It doesn't work. I also tried:

if [ $a == *--* ]
if [ $a == *---* ]
if [[ $a == *-* ]]
if [[ $a == *--* ]]
if [[ $a == *---* ]]

With no success...

Thanks in advance


Solution

  • Following piece of code brings problems

    a=$(tempfile)    
    echo "22:00:00-02:00" > $a
    

    Here you are writing to a file $a and then try to do string comparison.


    Try following

    a="22:00:00-02:00"
    
    if [[ $a == *-* ]]
    then
       echo "contains"
    fi