Bash Aliases

Brett Terpstra
Brett TerpstraPrincipal Developer, Oracle
Published: December 1, 2023

An alias in Bash (and most shells) is a way to run a long command using a short one. If you repeat a command often in the terminal, an alias can save you a lot of typing. Creating an alias in Bash (and most shells) is pretty easy.

Create an alias

To create an alias in Bash:

Bash
alias NAME="COMMAND TO PERFORM"

(Note that there is no space around the = symbol.)

For example, to create an alias that performs a long-format listing of files when you type ll, you would use:

Bash
alias ll="ls -l"

This will create an alias in the current shell that will be active until the shell is exited.

An alias literally replaces its name with its contents on the command line, so you can append additional arguments after the alias. In the example above, you might additionally want to display extended attributes, in which case you could call ll -@, which would translate to ls -l -@.

You can also create an alias that overrides a command by giving it the same name (an alias takes precedence over a command of the same name). If you always wanted ls to run with the -F flag (appending a symbol to indicate file type), you could use alias ls="ls -F".

Aliasing multiple commands

You can combine multiple commands in an alias by separating them with a semicolon, or by using &&, which will run the next command only if the previous command succeeds.

For example:

Bash
alias gpm="cd `git rev-parse --show-toplevel` 
 && git checkout main && git pull"

This will cd to the top-level git directory, check out the main branch, and run git pull. If any of these commands fail, the execution will be terminated before the next command.

Making aliases permanent

If you want your alias to be available in every shell you open, you'll need to add it to your ~/.bashrc or ~/.bash\_profile. (For other shells you'll need to determine what config files are loaded at shell login). Note that when adding aliases to a startup file, you need to source the file to activate it in your current session (e.g. source ~/.bash\_profile).

A note about Bash startup files:

.bash\_profile is loaded in interactive sessions and is a standard place for aliases and functions you want to use while logged into a shell. If you need an alias to exist in non-interactive shells, use ~/.bashrc.

Normally you would open the startup file in an editor (such as Vim or VS Code) and insert the line where it makes sense. To quickly add an alias to your .bash\_profile, you can just append it to the file using shell redirection:

Bash
echo 'alias ll="ls -l"' >> ~/.bash_profile

Alias Examples

CD to the top level of a Git repo

The point of an alias is to save you time typing, so short names that make sense to you are preferred. If you wanted an alias to switch to the top level of a git repository, for example, you could name it gt for "go top" instead of something longer and harder to type like go\_top.

Bash
alias gt="cd `git rev-parse --show-toplevel`"

This alias also demonstrates that you can nest executable statements within the alias using backticks or $() syntax.

Edit your Bash profile

If you're customizing your startup files frequently, you might want an alias to edit the file. You can use bp (bash profile) as an alias to call your editor of choice on the startup file you prefer:

Bash
alias bp="vim ~/.bash_profile"

Top aliases

If you frequently examine running processes, you can add some simple aliases for top as shortcuts:

Bash
# Sort by CPU
 alias cpu="top -o cpu"
 
 # Sort by memory
 alias mem="top -o rsize"

Quick directory climbing

Do you spend a lot of time typing cd ..? A couple aliases can make this easier:

Bash
alias ..="cd .."
 alias ...="cd ../.."
 alias ....="cd ../../.."

Now you can just type .. to cd to the parent directory, or .... to go three levels up in one easy command.

Copy the current directory to the clipboard

This works on macOS, for other OSs you'll need to replace pbcopy with a command line utility for your OS that can access the clipboard:

Bash
alias cpwd='pwd|tr -d "\n"|pbcopy'

Now running cpwd will put the path to the current directory in your clipboard, sans newline.

Get your external IP address

Want a quick way to check your external IP address?

Bash
alias ip="curl icanhazip.com"

Recursively delete Dropbox conflicted files

This will find all (XXXX's conflicted copy) files in the current directory and delete them:

Bash
alias rmdbc="find . -name \"*(*'s conflicted copy*)*\" -exec rm {} \;"

Creating Aliases with Arguments

Aliases are great if you have a command you repeat often, or one that you only need to append arguments to. If you need to interpolate arguments (insert them at points in the command other than the end), you may be better served by a function.

Functions in Bash are created with a name followed by a set of parentheses, with the actions of the function enclosed in curly brackets:

Bash
function_name() { 
   # function actions
 }

Inside a function, arguments can be referred to using $X, where X is the position of the argument. In the command dothis with this, $1 is "with" and $2 is "this." With this method, you can exert more control over where additional arguments are placed in the command.

Another benefit to functions is that you can combine arguments using double quotes without having to do so on the command line. One of my favorite functions is gg, which lets me create a git commit message without quoting:

Bash
gg() {
   git commit -v -a -m "$*"
 }

The $\ means "all arguments, unquoted". So now if I run gg initial commit (note the lack of quotes or flags), it will instead run git commit -vam "initial commit".

You can use functions anywhere you can use an alias, so include them in your .bashrc or .bash\_profile and save yourself typing time!

Written by
Brett Terpstra
Brett TerpstraPrincipal Developer, Oracle
Filed under

Related articles


Bash Comments

Comments will help make your scripts more readable

Reading User Input

Via command line arguments and prompting users for input

Curl Post Request

Use cURL to send data to a server

Bash If Statement

Learn how to use the if statement in Bash to compare multiple values and expressions.

Bash While Loop

Learn how to use and control the while loop in Bash to repeat instructions, and read from the standard input, files, arrays, and more.

Upload Files With curl

Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.

How To Copy A Directory In Linux

Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.