Search code examples
bashshellmatching

Shell string pattern matching : var="this is a test"; echo ${var#t?st} match nothing


I am studying bash shell scripting.

var="this is a test"; echo ${var#t?st}

or

var="this is a test"; echo ${var#test}

result

this is a test

I know that hashnet will delete the shortest or longest matching string. If the pattern is 'test' then the word 'test' matches, so the shortest or longest match should be 'test', so I think the result should be 'this is a'.

But the result is this is a test. This means pattern doesn't match at all. Could you explain why this is happen?


Solution

  • # deletes from the beginning of the string, % deletes from the end.

    echo "${var%t?st}"      # This is a
    echo "${var#[Tt]his }"  # is a test