Bash Aliases
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:
(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:
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:
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:
Alias Examples
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:
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:
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!