I am trying to write the dirname function in Bash so that it doesn't use any external commands.
function dirname() {
local path=$1
[[ $path =~ ^[^/]+$ ]] && dir=. || { # if path has no slashes, set dir to .
[[ $path =~ ^/+$ ]] && dir=/ || { # if path has only slashes, set dir to /
local IFS=/ dir_a i
read -ra dir_a <<< "$path" # read the components of path into an array
dir="${dir_a[0]}"
for ((i=1; i < ${#dir_a[@]}; i++)); do # strip out any repeating slashes
[[ ${dir_a[i]} ]] && dir="$dir/${dir_a[i]}" # append unless it is an empty element
done
}
}
[[ $dir ]] && printf '%s\n' "$dir" # print only if not empty
}
In order to remove any repeating /
from the path, I had to use the array logic. Is there a simpler way to do the same with Bash Parameter Expansion? I tried, but I don't seem to get it right.
Basically, I want to replace all occurrences of multiple consecutive slashes with a single slash each.