dirname is not a bash command -it is a separate binary program.
Here is the way to do it in bash:
Code:
DIRECTORY="${filename%/*}"
But, if the path has a trailing '/' then that won't work.
Code:
if [[ ${test:$((${#test}-1)):1} = '/' ]] ; then
filename=${filename:0:$((${#filename}-1))}
fi
DIRECTORY="${filename%/*}"
That's still not a perfect substitute for the real dirname command because it doesn't take into account the case of having just 'filename=/'
Here's a full version of 'dirname' in pure posix shell:
Code:
dirname()
{
_dirname "$1" && printf "%s\n" "$_DIRNAME"
}
_dirname()
{
_DIRNAME=$1;
strip_trailing_slashes;
case $_DIRNAME in
"")
_DIRNAME='/'
return
;;
*/*)
_DIRNAME="${_DIRNAME%/*}"
;;
*)
_DIRNAME='.'
;;
esac;
strip_trailing_slashes;
}
strip_trailing_slashes ()
{
while [ "${_DIRNAME%/}" != "$_DIRNAME" ]; do
_DIRNAME=${_DIRNAME%/};
done
}
dirname "$@"