Ayusman
Ayusman

Reputation: 8729

Understanding linux shell script expression

Hi I have a small script (script name is: test1.sh) that looks like this

PRG=$0
data=`expr $PRG : '.*\/.*'`
echo $data

When I run this I see output as

10

I could not understand the regular expression written in the second line of the script. What would that mean?

Upvotes: 1

Views: 195

Answers (2)

perreal
perreal

Reputation: 98108

The expression returns a non-zero value if there is a / within the relative filename of the script ($0 in sh). If you execute the script like this: sh ../../script.sh, it outputs 15, which is the total length of "../../script.sh". It matches "../../" with '.*\/ and matches script.sh with the .* part.

Upvotes: 1

Zombo
Zombo

Reputation: 1

From

If the match succeeds the `:'
     expression returns the number of characters
     matched.

So the 10 is likely

./test1.sh
^^^^^^^^^^
||||||||| \
123456789 10

Upvotes: 2

Related Questions