Shell parameter extension: how can I get the file name without part of the directory?

I wrote a makefile and suggested that I have the following:

FILES = file1.py \
    folder1/file2.py \
    folder2/file3.py

And I have a for loop:

    -@for file in $(FILES); do \
           echo $${file/folder1\/}; \
    done

The above text will print:

file1.py
file2.py
folder2/file3.py

The output I want is:

file1.py
file2.py
file3.py

I looked through the documentation on shell extensions, but have not yet found a way to handle this. Can I learn how to change the code to get the correct result? Any help would be greatly appreciated.

EDIT: syntax

+3
source share
2 answers

Try to use echo $${file##*/}. This will give only part of the file name without any changes to the last slash.

+2
source

You already accepted the @Jens shell-style answer, but I suggest make-style :

-@for file in $(notdir $(FILES)); do \
       echo $${file}; \
done
+5

All Articles