Company Logo

Bisht Bytes

Using an alias and shell function

Published On: 21 Jul 2024
Reading Time: 6 minutes

Overview


Shell Aliases

An alias is a shortcut for a command or a series of commands. Aliases are used to simplify and customize command-line usage.

Creating Aliases

To create an alias, use the alias command followed by the name of the alias and the command it represents.

alias alias_name='command'

Example

alias ll='ls -la'

Persistent Aliases

To make aliases persistent across sessions, you need to add them to your shell's configuration file (e.g., .bashrc, .bash_profile, .zshrc).

Add the following line to ~/.bashrc or ~/.zshrc:

alias ll='ls -la'

Then, reload the configuration file:

source ~/.bashrc

When you need to chain multiple commands and alias limitations become apparent, using shell functions as alternatives can be very powerful. Here’s a guide on creating alias alternatives using shell functions:

Creating Alias Alternatives with Shell Functions

Aliases are simple and useful for short commands, but they lack flexibility when dealing with complex command chains, parameters, and logic. Shell functions provide a more powerful alternative.

Example: Complex Command Chain

Suppose you have a complex command chain like this:

git status && git add . && git commit -m "commit message" && git push

Creating an alias for this would be cumbersome and inflexible, especially if you need to add parameters. A shell function is more suitable.

Step-by-Step Guide

Define the Shell Function

Encapsulate the commands within a function. This allows you to handle parameters and add conditional logic if needed.

git_commit_and_push() {
  local message=$1
  if [ -z "$message" ]; then
    echo "Commit message is required"
    return 1
  fi

  git status
  if [ $? -ne 0 ]; then
    echo "Git status failed"
    return 1
  fi

  git add .
  if [ $? -ne 0 ]; then
    echo "Git add failed"
    return 1
  fi

  git commit -m "$message"
  if [ $? -ne 0 ]; then
    echo "Git commit failed"
    return 1
  fi

  git push
  if [ $? -ne 0 ]; then
    echo "Git push failed"
    return 1
  fi
}

Add the Function to Your Shell Configuration File

To make the function available in all your shell sessions, add it to your ~/.bashrc, ~/.bash_profile, or ~/.zshrc file.

# Add this function to your shell configuration file
git_commit_and_push() {
  local message=$1
  if [ -z "$message" ]; then
    echo "Commit message is required"
    return 1
  fi

  git status
  if [ $? -ne 0 ]; then
    echo "Git status failed"
    return 1
  fi

  git add .
  if [ $? -ne 0 ]; then
    echo "Git add failed"
    return 1
  fi

  git commit -m "$message"
  if [ $? -ne 0 ]; then
    echo "Git commit failed"
    return 1
  fi

  git push
  if [ $? -ne 0 ]; then
    echo "Git push failed"
    return 1
  fi
}

Reload the Shell Configuration

Source the configuration file to apply the changes without restarting your terminal.

source ~/.bashrc    # or source ~/.zshrc for Zsh users

Use the Function

Now you can use the function in your terminal.

git_commit_and_push "Initial commit"

Combining Aliases and Functions

You can combine aliases and functions to create more complex and reusable shell commands.

alias myalias='myfunction'

myfunction() {
  echo "This is a combined alias and function"
}

Getting details of the alias or function

To print the function defination on the cli console, use:

declare -f <function-name>

To print the alias defination on the cli console, use:

alias <alias-name>

Summary

Using shell functions instead of aliases provides several advantages for complex command chains:

  • Parameter Handling: Functions can accept parameters, making them more flexible.
  • Conditional Logic: You can include error handling and conditional statements.
  • Reusability: Functions can be reused and included in scripts more easily.
  • Readability: Functions make your scripts more readable and maintainable.

By using shell functions, you can create more powerful and flexible command-line tools that go beyond the limitations of aliases. This approach enhances your productivity and allows you to automate complex tasks efficiently.


Page Views: -