Issue
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.
Solution
It's removing everything up to the last /
in the value of $FILE
. From the Bash Manual:
${parameter#word}
${parameter##word}
Theword
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 ofparameter
, then the result of the expansion is the value ofparameter
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/*
Answered By - Barmar Answer Checked By - Timothy Miller (WPSolving Admin)