Created
December 15, 2022 21:25
-
-
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
This file contains hidden or 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() { | |
# 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