Skip to content

Instantly share code, notes, and snippets.

@AdrianAcala
Created December 15, 2022 21:25
Show Gist options
  • Save AdrianAcala/d5bfc0ca7f380eba65b0a68609da952e to your computer and use it in GitHub Desktop.
Save AdrianAcala/d5bfc0ca7f380eba65b0a68609da952e to your computer and use it in GitHub Desktop.
Bash function that takes a directory name and creates it then enters that folder
function mkcd() {
# Check if the directory already exists
if [ -d "$1" ]; then
# If the directory already exists, print an error message
printf "Error: directory '%s' already exists\n" "$1"
else
# If the directory does not already exist, create it
mkdir "$1"
# Check if the directory was successfully created
if [ $? -eq 0 ]; then
# If the directory was successfully created, cd into it
cd "$1"
else
# If there was an error creating the directory, print an error message
printf "Error: failed to create directory '%s'\n" "$1"
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment