I'm learning some shell scripting but came across this line: link=`expr "$ls" : '^.*-> \(.*\)$' 2>/dev/null` Where ls=$0 Can someone explain it to me pls?

Hey There,

Forgetting about what the "ls" variable evaluates as, it's a pretty simple expression that just looks confusing. It's using expr's ":" string comparison operator.

on the left side you have "$ls" which you say equal $0, which is traditionally the command name if you're calling a script. So, we'll assume the left side resolves to:
"/bin/bash"

On the right side of the ":" is where the regular expression matching occurs. Normally, depending on your version of expr, the number of bytes or characters matched as the result of the total equation, is returned. Since this regular expression has a sub-expression trap in it "\(" whateer "\)", just like in sed, if the equation doesn't fail (in which case it would return 0) it should return that sub-expression.

To deconstruct the left-hand-side regular expression, you can check the manpage for "regex", but just so you have all the info in one place, the left hand side, bit by bit is:

"^" - This indicates that the pattern match begins at the very beginning position of the line or variable.
".*" - this indicates zero or more of any character - alpha or numeric.
"->" - this appears to be a straightforward exact pattern match. the "-" character is generally only special in between brackets when expressing a range.
"\(.*\)" - this is a combination of the ".*" regular expression (as previous) surrounded by the sub-expression markers (as previous)
"$" - the last character in the regular expression (which makes sense) indicates that the match ends at the very last position of the line or variable.
The left hand side of the equation is in double quotes, which allows for the variable interpolation to occur, while the right hand side is in single quotes.
"2>/dev/null" - this just sends all STDERR output to nowhere, so you won't have to see it.

So, in plain english, the equation is saying.

If "/bin/bash" (what we've assumed $ls, which equals $0 evaluates to.. could be something different) matches within the regular expression on the right hand side of the colon, that match would get output, otherwise 0 would be returned.

My guess is this equation is actually looking for something like a command prompt (because of the hardcoded "->". Obviously "/bin/bash" would never match. It would work all the way through "^.*" - but would stop matching there.

If the left side evaluated to, say, "/home/user1-> # " or something like that, the expression would return " # " since
"^.*->" would match "/home/user1->" or "whatever->"
and "\(.*\)$" would make the entire rest of the line match the sub-expression which is simply zero or more of any alpha or numeric character (i.e. the rest of the string, which - in this case - would be " # ")

Hope that was more helpful than confusing ;)

Happy Holiday,

Mike

I'm learning some shell scripting but came across this line: link=`expr "$ls" : '^.*-> \(.*\)$' 2>/dev/null` Where ls=$0 Can someone explain it to me pls?

Ah! Thanks very much. Makes sense now.

Glad to help out :)

, Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.