Following one example of the book << Learning the Bash Shell >> (O'Reilly),

pathname="/home/cam/book/long.file.name"
echo ${pathname##/*/}
echo ${pathname#/*/}

The expected result should be long.file.name, since ## remove the longest prefix which matches the patter /*/.

However, when I put these three lines inside a script file and run it inside bash, there is no result displayed. But type in these two lines one by one works and shows the expected result.

I wonder if there is any setting related to usage of this operator ## inside executable script.

(Using ubuntu\trusty64 within vagrant.)

Thanks.

UPDATE

The code works fine, the other part of the code affects the results.

In Addition

${path##*/} is a better choice as equivalent to basename command.

1 Answers

1
RavinderSingh13 On

Though echo ${pathname##/*/} works fine for me but IMHO you should try following.

echo ${pathname##*/}

Which means you are saying bash with help of regex to remove/substitute everything from starting till last occurrence of / with NULL.