Last active
October 29, 2020 11:08
-
-
Save solarshado/1202b1511baf4a88766e5c057ee98591 to your computer and use it in GitHub Desktop.
a bash function enabling "cd -f" to create non-pre-existent directories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mkcd { | |
local CD_ARGS DIR MKDIR=0 | |
while (($#)); do | |
if [[ $1 == "-f" ]]; then | |
MKDIR=1; | |
else | |
CD_ARGS+=("$1") | |
if [[ "$1" == "${1##-}" ]]; then | |
DIR+=("$1") | |
fi | |
fi | |
shift | |
done | |
if [[ ${#DIR[@]} -gt 1 ]]; then | |
echo "too many dir-like args" | |
return 1 | |
fi | |
if [[ $MKDIR == 1 ]]; then | |
mkdir -v -p "$DIR" && command cd "${CD_ARGS[@]}" | |
else | |
command cd "${CD_ARGS[@]}" | |
fi | |
} | |
# optional: | |
#alias cd=mkcd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment