Bash basename syntax
bash
Solution
First, your syntax is wrong:
file=$( basename "$1" )
Second, this is the correct expression to get the file name's (last) extension:
ext=${file##*.}
Problem
I'm trying to write a script that takes the basename of an argument, then checks if there is an extension in that argument. If there is, it prints the extension. Here is my code: ``` file=basename $1 ext=${file%*} echo ${file#"$stub"} echo $basename $1 ``` I'm echoing the final $basename $1 to check what the output of basename is. Some tests reveal: ``` testfile.sh one.two ./testfile: line 2: one.two: command not found one.two testfile.sh ../tester ./testfile: line 2: ../tester: No such file or directory ../tester ``` So neither $basename $1 are working. I know it's a syntax error so could someone explain what I'm doing wrong? EDIT: I've solved my problem now with: ``` file=$(basename "$1" ) stub=${file%.*} echo ${file#"$stub"} ``` Which reduces my argument to a basename, thank you all.