------------------------------------------------------------------------------- Expr is an old program, but has many problems. For example sub-string matching fails if the variable is a keyword like "match", or "index" WARNING: expr is not a built-in but works for old Bourne sh This works reasonably well - most of the time word=abcde expr "$word" : ".\(.*\)" bcde But WILL fail is word is a "expr" keyword word=match expr "$word" : ".\(.*\)" word=index expr "$word" : ".\(.*\)" 0 word=substr expr "$word" : ".\(.*\)" expr: syntax error Using "expr match" with the word "match" will also fail word=match expr match "$word" ".\(.*\)" expr: syntax error The problem is "match" is a keyword, which confuses "expr" argument parsing. The solution is to always start "expr" with '+' and use the older ':' form word=match expr + "$word" : ".\(.*\)" atch word=index expr + "$word" : ".\(.*\)" ndex word=substr expr + "$word" : ".\(.*\)" ubstr An alturnative and more portible solution for this specific expression is to modify the expression so the input never can match a keyword word=match expr "x$word" : "x.\(.*\)" atch This Cavat can be very important for ALL expressions, NOT just a string test. ------------------------------------------------------------------------------- OLD MacOSX expr is worse MacOsX "expr" using "match" however will fail (an old version of expr) This is fixed after Bash upgrades following Shell Shock expr match "this_is_a test" 'this' expr: syntax error You must use the ':' sub-string test instead expr "this_is_a test" : 'this' 4 Unfortunatally adding the '+' to the start also fails Arrggghhhh.... expr + "this_is_a test" : 'this' expr: syntax error Best idea is modify the expression using the portible technique shown above word="this_is_a test" expr \( "x$word" : "xthis" \) - 1 4 -------------------------------------------------------------------------------