Created
June 5, 2024 00:35
-
-
Save minuz/ca1e294f87f10d87807670ae87d77942 to your computer and use it in GitHub Desktop.
Add Command alias on MacOS
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
| #!/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." |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE: If you copy this script, before executing, you must set permissions to it: