Shell parameter expansion: how can I get the file name without the directory part?
-
06-06-2021 - |
質問
I was writing a makefile and suppose I have the following;
FILES = file1.py \
folder1/file2.py \
folder2/file3.py
And I have the following for loop:
-@for file in $(FILES); do \
echo $${file/folder1\/}; \
done
The above will print:
file1.py
file2.py
folder2/file3.py
The output I want is:
file1.py
file2.py
file3.py
I've looked at some documentation about shell expansion but couldn't yet find a way to deal with this. May I know how I should change the code to get the correct output? Any help will be very much appreciated.
EDIT: syntax
解決
Try using echo $${file##*/}
. This will give only the filename part without anything up to the last slash.
他のヒント
You've already accepted @Jens's shell-style answer, but I'll suggest a make-style solution:
-@for file in $(notdir $(FILES)); do \
echo $${file}; \
done
所属していません StackOverflow