In my script I’m trying to take a string, then output the extension of a string if it has one. So essentially I take the basename of a file, then output anything that comes after a period.
What’s the syntax to do something like this?
For example
dotcutter.sh
file=testfile.jpg
the script should output .jpg
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.
Notice that it doesn’t work if the file has no extension. If that’s important then try this two-step solution:
Or this regex-based one, which will only call
echoif there’s actually an extension. (&&is shorthand for “if then”.)See also: