Skip to content

Instantly share code, notes, and snippets.

@solarshado
Last active October 29, 2020 11:08
Show Gist options
  • Save solarshado/1202b1511baf4a88766e5c057ee98591 to your computer and use it in GitHub Desktop.
Save solarshado/1202b1511baf4a88766e5c057ee98591 to your computer and use it in GitHub Desktop.
a bash function enabling "cd -f" to create non-pre-existent directories
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