Skip to content

Instantly share code, notes, and snippets.

@minuz
Created June 5, 2024 00:35
Show Gist options
  • Select an option

  • Save minuz/ca1e294f87f10d87807670ae87d77942 to your computer and use it in GitHub Desktop.

Select an option

Save minuz/ca1e294f87f10d87807670ae87d77942 to your computer and use it in GitHub Desktop.
Add Command alias on MacOS
#!/bin/bash
# Check if the correct number of arguments are provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <alias_name> <alias_command>"
exit 1
fi
# Get the alias name and command from the arguments
ALIAS_NAME="$1"
ALIAS_COMMAND="$2"
# Define the target file
TARGET_FILE="$HOME/.zshrc"
# Add the alias to the end of the file if it doesn't already exist
if grep -Fxq "alias $ALIAS_NAME=\"$ALIAS_COMMAND\"" "$TARGET_FILE"; then
echo "Alias '$ALIAS_NAME' already exists in $TARGET_FILE"
else
echo "alias $ALIAS_NAME=\"$ALIAS_COMMAND\"" >>"$TARGET_FILE"
echo "Alias '$ALIAS_NAME' added to $TARGET_FILE"
fi
# Source the .zshrc file to apply the changes immediately
source "$TARGET_FILE"
echo "$TARGET_FILE sourced. Alias '$ALIAS_NAME' is now available in the current session."
@minuz
Copy link
Author

minuz commented Jun 5, 2024

NOTE: If you copy this script, before executing, you must set permissions to it:

chmod +x <dir>/add-alias.sh
<dir>/add-alias.sh create-branch git checkout main && git pull --rebase && git checkout -b
create-branch feature/something

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment