0

I came across a shell script like the following:

for FILE_PATH in `ls some/directory`
do
    export FILE=${FILE_PATH##*/}
done

What exactly is the "##*/" doing? When I echo ${FILE} and ${FILE_PATH}, I don't see any difference. Is this to handle unusually named files?

More generally, how would I go about figuring out this type of question for myself in the future? Google was completely useless.

calmcc
  • 189
  • 7
  • 1
    For anything to do with Bash, if you don't know the term to search for, searching the [reference manual](https://www.gnu.org/software/bash/manual/bash.html) is often a good idea. `##` shows up under [*Shell parameter expansion*](https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion). – Benjamin W. Jul 28 '22 at 19:53

1 Answers1

1

It's removing everything up to the last / in the value of $FILE. From the Bash Manual:

${parameter#word}
${parameter##word}
The word is expanded to produce a pattern and matched according to the rules described below (see Pattern Matching). If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the ‘#’ case) or the longest matching pattern (the ‘##’ case) deleted.

You're not seeing any difference in this case because when you list a directory it just outputs the filenames, it doesn't include the directory portion, so there's nothing to remove. You would see the difference if you did:

for FILE in some/directory/*
Barmar
  • 741,623
  • 53
  • 500
  • 612