for i in $@ ; do if [ -z ${i##*.c} ]; then echo "YES: $i" fi done $ ./test.sh .c .c-and-more before.c-and-after foo.h foo.c barc foo.C YES: .c YES: foo.c $
Explanation (thanks jpaugh ):
- Iterating over command line arguments:
for i in $@ ; do for i in $@ ; do - The main trick here:
if [ -z ${i##*.c} ]; then if [ -z ${i##*.c} ]; then . Here we check if the string length ${i##*.c} is equal to zero. ${i##*.c} means: take the value of $ i and remove the substring according to the pattern "* .c". If the result is an empty string, then we have the suffix .c ".
Here, if there is any additional information from a human bash, the section Parameter Expasion
${parameter
source share