Bash allows you to create your own aliases for commands. We've seen this introduced in Chapter 14, Scheduling and Logging, but for day-to-day tasks, it is worth exploring a little bit further. The syntax is pretty straightforward:
alias name=value
In this syntax, alias is the command, name is how the alias will be called by you on the Terminal, and value is what is actually called when you call the alias. For interactive work, this could look like the following:
reader@ubuntu:~$ alias message='echo "Hello world!"'
reader@ubuntu:~$ message
Hello world!
We created the alias message, which actually does echo "Hello world!" for us when called. For those of you with a little bit more experience, you've no doubt been using the "command" ll for some time now. As you might (or might not) remember, this is a common default alias. We can print currently set aliases with the -p flag:
reader@ubuntu:~$ alias -p
<SNIPPED>
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
alias message='echo "Hello world!"'
As you can see, by default we have some aliases set and the one we just created is there as well. What is even more interesting, is the fact that we can use alias to override a command, such as ls above. All the times we used ls in the book's examples, we've actually been executing ls --color=auto! The same goes for grep, as the print clearly shows. The ll alias quickly allows us to use common, almost essential flags for ls. However, you would do well to realize that these aliases are distribution-specific. Take a look at the ll alias on my Arch Linux host machine for example:
[tammert@caladan ~]$ alias -p
alias ll='ls -lh'
<SNIPPED>
This is different from our Ubuntu machine. At the very least, that begs the question: where are these default aliases set? If you remember our explanation about /etc/profile, /etc/bash.bashrc, ~/.profile, and ~/.bashrc (in Chapter 14, Scheduling and Logging ), we know that these files are the most likely candidates. From experience, you can expect most aliases to be in the ~/.bashrc file:
reader@ubuntu:~$ cat .bashrc
<SNIPPED>
# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
<SNIPPED>
If you have commands that you often use, or flags that you would like to include "by default," you can edit your ~/.bashrc file and add as many alias commands as you like. Any commands in the .bashrc file are run when you log in. If you want to make an alias available system-wide, the /etc/profile or /etc/bash.bashrc files would be better choices to include your alias command in. Otherwise, you'd have to edit the personal .bashrc files of all users, current and future (which is not efficient, so you shouldn't even consider this).