Creating Shortcuts with Aliases and Functions

Learn how to create commands using aliases and functions.

Bash allows us to define our own versions of commands, either through aliases or through functions. Aliases let us create shortcuts for commands or override the default options for existing commands. We define them with the alias command, but it’s better to define them in our ~/.bashrc file. Let’s define some aliases.


Let’s create an alias for ls -alh so we can avoid typing it out. We add this line to the end of the ~/.bashrc file to define an alias called ll:

alias ll='ls -alh'

We then source the file to apply the changes:

$ source ~/.bashrc

Now, we try running the ll alias:

$ ll

Run the complete code on the terminal below for practice.

cat << "EOF" > /.bashrc
# START:return
[ -z "$PS1" ] && return
# END:return

# START:histcontrol
# END:histcontrol

# START:histappend
shopt -s histappend
# END:histappend

# START:histsize
# END:histsize

# START:histignore
# END:histignore

# START:editor
export EDITOR=nano
export VISUAL=nano
# END:editor

# START:path
export PATH="~/bin:$PATH"
# END:path

# START:colorprompt
USERCOLOR='\[$(tput setaf 6)\]'  # cyan
RESET='\[$(tput sgr0)\]'

PS1="${USERCOLOR}\u${RESET}@\h:\w \\$ "
# END:colorprompt

# START:alias_ll
alias ll='ls -alh'
# END:alias_ll
source /.bashrc

Get hands-on with 1200+ tech skills courses.