-2

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
  • 1
    Works fine for me. Try running bash -x scriptname and then edit your question and copy-and-paste the results as an update to the question.
    – John1024
    Apr 18, 2019 at 3:43

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.