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"
Run in Warp
(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"
Run in Warp
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"
Run in Warp
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
Run in Warp
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`"
Run in Warp
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"
Run in Warp
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"
Run in Warp
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 ../../.."
Run in Warp
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'
Run in Warp
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"
Run in Warp
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 {} \;"
Run in Warp
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
}
Run in Warp
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 "$*"
}
Run in Warp
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
Principal Developer, Oracle
Filed Under
Related Articles
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.
POST JSON Data With Curl
How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.
Use Cookies With cURL
Learn how to store and send cookies using files, hard-coded values, environment variables with cURL.
Loop Through Files in Directory in Bash
Learn how to iterate over files in a directory linearly and recursively using Bash and Python.
How To Use sudo su
A quick overview of using sudo su
Generate, Sign, and View a CSR With OpenSSL
Learn how to generate, self-sign, and verify certificate signing requests with `openssl`.
How to use sudo rm -rf safely
We'll help you understand its components
How to run chmod recursively
Using -R is probably not what you want
Run Bash Shell In Docker
Start an interactive shell in Docker container
Curl Post Request
Use cURL to send data to a server
Reading User Input
Via command line arguments and prompting users for input