I seem to have failed at something pretty simple, in bash. I have a string variable that holds the full path to a directory. I\'d like to assign the last two di
I prefer to use the builtins as much as I can, to avoid create unnecessary processes. Because your script may be run under Cygwin or other OS whose process creation are very expensive.
I think it's not so lengthy if you just want to extract two dirs:
base1="${DIRNAME##*/}"
dir1="${DIRNAME%/*}"
DIRNAME2="${dir1##*/}/$base1"
This can also avoid special char problems involved in executing another commands.