Skip to main content
added 2 characters in body
Source Link
#!/bin/bash
regex='([^/.]+)([.]zip)?$'
path="/home/quid/Downloads/file.zip"

if [[ $path =~ $regex ]]
then
     echo "Found a match"
     echo $"${BASH_REMATCH[1]}"
fi

Using Charles' regex and ${BASH_REMATCH[1]} gives me just the file name without the extension.

#!/bin/bash
regex='([^/.]+)([.]zip)?$'
path="/home/quid/Downloads/file.zip"

if [[ $path =~ $regex ]]
then
     echo "Found a match"
     echo ${BASH_REMATCH[1]}
fi

Using Charles' regex and ${BASH_REMATCH[1]} gives me just the file name without the extension.

#!/bin/bash
regex='([^/.]+)([.]zip)?$'
path="/home/quid/Downloads/file.zip"

if [[ $path =~ $regex ]]
then
     echo "Found a match"
     echo "${BASH_REMATCH[1]}"
fi

Using Charles' regex and ${BASH_REMATCH[1]} gives me just the file name without the extension.

Source Link

#!/bin/bash
regex='([^/.]+)([.]zip)?$'
path="/home/quid/Downloads/file.zip"

if [[ $path =~ $regex ]]
then
     echo "Found a match"
     echo ${BASH_REMATCH[1]}
fi

Using Charles' regex and ${BASH_REMATCH[1]} gives me just the file name without the extension.