#!/usr/bin/env bash # set_overwrite_alias - A Bash function to alias a command while preserving the original. # # Usage: # set_overwrite_alias # # Parameters: # new_command: The command to alias to (e.g., "bat"). # alias_name: The alias name to override (e.g., "cat"). # # Behavior: # - Checks if the new command exists. # - If it exists and the backup alias (alias_name with an underscore appended) isn't set, # it creates a backup alias pointing to the original command. # - Sets the alias (alias_name) to use the new command. # # Example: # To alias "bat" as "cat" and preserve the original "cat" as "cat_", run: # set_overwrite_alias bat cat set_overwrite_alias() { local new_cmd="$1" local alias_name="$2" if [ -z "$new_cmd" ] || [ -z "$alias_name" ]; then echo "Usage: set_overwrite_alias " return 1 fi # Check that the new command exists. if type "$new_cmd" >/dev/null 2>&1; then # Define a backup alias by appending an underscore (e.g., "cat_" for "cat") local backup_alias="${alias_name}_" # If the backup alias isn't already set, create it pointing to the original command. if ! command -v "$backup_alias" &>/dev/null; then alias "$backup_alias"="$(which "$alias_name")" fi # Create the alias: e.g., alias "cat" to "bat". alias "$alias_name"="$new_cmd" fi }