Intro

In this guide, I will be going over the following subjects:

  1. Aliases and how to create them
  2. Functions, parameters and some bash specific syntax
  3. Saving everything in a file and making them persistent

Aliases

Aliases, as the name suggests, are aliases for other commands! By using aliases you give any built-in (or custom) bash command whatever name you want. The use case for aliases are not only giving different commands different names. You can also set an alias for a complex command which utilizes many flags.

An example is as follows:

# We want to give this frequently used command a shorter name, since we don't want to write -Al all the time.
ls -Al

The -Al part of the command is called flags. They provide additional functionalities or change the behavior of the commands.

In this case, the -A flag is used to show hidden files and -l is for list view. Note that -Al is a combination of two flags and it can be represented as -A -l as well.

On a side note, you can learn about the flags of any command using man.

For the ls example, we have:

# Usage: man <command_name>
man ls

Getting back to aliases, let's say you want to run ls -Al whenever you type ll. You can do this as follows:

# Beware of spaces around =
alias ll='ls -Al'
# And then, you can run
ll

Congratulations! You have created your first alias. However, if you close your terminal, the alias will be deleted. We will get back to this later on. Most popular distributions already provide you with some QoL improving aliases.

You can list all of them by running alias -p.

Before moving on to the next topic, I will provide you with some of my aliases to hopefully give you inspiration to create your own aliases.

alias ls='ls --color=auto'
alias ll='ls -Al'
alias cls='clear'
alias ..='cd ..'
alias grep='grep -n --color=auto'    # Colored and line number grep
alias ip='ip --color=auto'
alias diff='diff --color=auto'
alias tmux='tmux -f $HOME/.config/Scripts/tmux.conf'    # You can also specify config paths by using aliases

Functions

Functions are similar to aliases, where you can, for instance, create a function called ll and inside of it call ls -Al and achieve the same result as the previous alias example. However, there is more to functions. With functions, you can have parameters or inputs and execute more complex things compared to aliases. Think of it as any other programming language.

To write out first function, we first need to create a file and edit it.

You can use touch <file_name> to create an empty file and edit it with your editor of choice. The file's extension is not important.

For this example, I will create a file called 'functions'.

touch functions
# Editing the 'functions' file

#!/bin/bash
function first_function() {
    echo I got $# parameters.
    echo First parameter is $1.
}

This function will print out the number of parameters it received, as well as the first parameter.

The #!/bin/bash line is a special line called "shebang". It is defined at the beginning of a script file and tells the system to use '/bin/bash' to execute the script.

After saving the file, must first source it. You can do it by source file_name.

Taken from the man page, this command lets us "Execute commands from a file in the current shell".

source functions

first_function Hello World!    # Calling our function with parameters 'Hello' and 'World!'

# Output
I got 2 parameters.
First parameter is Hello.

To print every input parameter, we can utilize loops as follows:

# Editing the 'functions' file

#!/bin/bash
function first_function() {
    echo I got $# parameters.
    for i in $@
    do
        printf "$i "  # Prints elements without new lines at the end
    done
    echo  # New line just for prettier output
}

Since we changed our function, we have to source our 'functions' file and then call our function.

source functions
first_function Hello World!

# Output
I got 2 parameters.
Hello World!

We could have just used echo $@ to achieve the same thing, however, I wanted to show you the bash loops.

Great! We wrote our first function that takes arguments. Now some bash related info, mainly the special variables.

There are many special variables, here are some to get you started:

$0: This variable holds the name of the script itself, including the path, if provided.
$1, $2, $3, ...: These variables represent the command-line arguments passed to the script. $1 holds the first argument, $2 holds the second, and so on.
$#: This variable stores the number of command-line arguments passed to the script.
$@: It represents all the command-line arguments as a list. This is similar to using "$1", "$2", etc., but allows you to reference all arguments collectively.
$?: After a command is executed, this variable contains the exit status of the last executed command. A value of 0 typically indicates success, while non-zero values indicate errors.
$$: This variable contains the process ID (PID) of the current script or shell.

Now, I will provide some of my favorite functions to hopefully inspire you to make your own!

# Creates a directory and if the creation is successful, navigates to the newly created directory
function mkdircd() {
    mkdir -p "$1" && cd "$1"
}

# fzf + vim. Performs a fzf search for a file and opens it with vim
vif() {
    if [[ $# -gt 0 && $1 == '-h' ]]; then
        file=$(find . 2> /dev/null | fzf)
    else
        file=$(fzf)
    fi
    if [[ $? != 0 ]]; then
        return 0
    else
        vim $file
    fi
}

Persistency

We learned about aliases and functions. However, when we close our terminal, our aliases go away and for the functions, we need to source them every time.

A common method to solve this problem is to create a file called aliasesrc (you can name the file whatever you want. This name is somewhat the norm). "rc" stands for "Run Commands". Again "rc" is the norm and "rc" files are often used to customize the behavior and appearance of software and the user's shell environment.

So, we will stick to the conventions.

After creating your aliasesrc file, you can put all your aliases and functions inside of it.

You can place this file where ever you want. I tend to place it inside of .config directory, which is in your $HOME directory. After you placed the file, it is time to modify your .bashrc file to source our "rc" file when we open the terminal. (Note: Depending on the distro, you might have .zshrc or something other than .bashrc)

# Inside your .bashrc add the following line
source $HOME/.config/aliasesrc

# Alternatively, you can use ~ instead of $HOME
# In that case, add this
source ~/.config/aliasesrc

And we are done! Now, every time you open your terminal, your aliases and functions will be ready to run!

Thanks for reading!