How to split path by last slash?

Use basename and dirname, that's all you need.

part1=$(dirname "$p")
part2=$(basename "$p")

A proper 100% bash way and which is safe regarding filenames that have spaces or funny symbols (provided inner_process.sh handles them correctly, but that's another story):

while read -r p; do
    [[ "$p" == */* ]] || p="./$p"
    inner_process.sh "${p%/*}" "${p##*/}"
done < list.txt

and it doesn't fork dirname and basename (in subshells) for each file.

The line [[ "$p" == */* ]] || p="./$p" is here just in case $p doesn't contain any slash, then it prepends ./ to it.

See the Shell Parameter Expansion section in the Bash Reference Manual for more info on the % and ## symbols.


I found a great solution from this source.

p=/foo/bar/file1
path=$( echo ${p%/*} )
file=$( echo ${p##*/} )

This also works with spaces in the path!