Files
dotfiles/functions/set_alias_with_backup
2025-02-10 18:16:59 +02:00

51 lines
1.6 KiB
Bash

#!/usr/bin/env bash
# set_alias_with_backup - A Bash function to alias a command while preserving the original.
#
# Usage:
# set_alias_with_backup <alias_name> <new_command>
#
# Parameters:
# alias_name: The alias name to override (e.g., "cat").
# new_command: The command to alias to (e.g., "bat").
#
# 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_alias_with_backup cat bat
set_alias_with_backup() {
local alias_name="$1"
local new_cmd="$2"
# Allow for extra parameters to be passed to the new command.
shift 2
local extra_params="$*"
# Check that both arguments are provided.
if [ -z "$alias_name" ] || [ -z "$new_cmd" ]; then
echo "Usage: set_alias_with_backup <alias_name> <new_command> [extra_params]"
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 ! type "$backup_alias" >/dev/null 2>&1; then
alias "$backup_alias"="$(which "$alias_name")"
fi
# Create the alias: e.g., alias "cat" to "bat" and pass any extra params.
alias "$alias_name"="$new_cmd $extra_params"
fi
}