Terminus by Warp
Bash Aliases

Bash Aliases

Brett Terpstra
Brett Terpstra
Principal Developer, Oracle

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:

 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:

 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:

 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:

 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.

 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:

 alias bp="vim ~/.bash_profile"
  

Top aliases

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

 # 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:

 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:

 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?

 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:

 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:

 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:

 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!

Experience the power of Warp

  • Write with an IDE-style editor
  • Easily navigate through output
  • Save commands to reuse later
  • Ask Warp AI to explain or debug
  • Customize keybindings and launch configs
  • Pick from preloaded themes or design your own
brew install --cask warp
Copied!
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist or join the Windows waitlist
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.