Search code examples
csh

How check if variable contains substring in c shell?


I am trying to check if a variable contains a specific substring. The test value is "test_tpTest171_ess"

 if ($test =~ "Test171") then
    echo "statement is working"

Also tried:

 $test == "*Test171*"
 $test == *Test171*

Solution

  • What you Also tried would have worked if you had used the correct operator.

    The glob-pattern on the right hand side of =~ must match the whole left hand operand, so if you want to allow extra text before and after Test171, you have to add * there:

    if ($test =~ *Test171*) echo "statement is working"