From the course: Linux Tips

Bash aliases and functions - Linux Tutorial

From the course: Linux Tips

Bash aliases and functions

- [Instructor] When we're using Bash, we often use commands that can be tedious to type out, either because they're long or because they're fairly complex. And sometimes, we want to use a command in a particular way most of the time and skip having to type out cryptic options every single time. Bash allows us to define aliases, which act like shortcuts to Bash commands. In fact, some aliases come built in to most installations of Bash to begin with. To create and view aliases, we use the alias keyword. The ll command here on my system is an alias to ls with a few options. This is the output of ll and this is the output of ls. I'll clear the screen. To take a look at what ll really is, I'll write alias ll and I can see that it's calling ls-alF. A, to list all files, l for a long listing oriented vertically instead of horizontally, and, with capital F, to classify things. That'll add a slash at the end of directories and so on. That helps us to see what kind of thing an entry in a list is. So let's say you like to use the very long group directories first option when looking at your files and you want to see the output in a list. I'll write, alias lg="ls -l - -group-directories-first". This creates an alias called lg that does just that. Now I can write lg and see the output the way that I like. I could even recall this and set the alias to ls itself, if I don't want to be bothered with remembering my custom command. Generally, you don't want to override a system command with an alias, but you can. To get the original meaning of the command, you just type the command with a slash in front of it. To edit an alias, you can just reassign the alias. Let's add the h option here for human readable sizes. There we go, and when you're done with an alias, you can remove it with the unalias command. Now ls is back to working how it did before. Aliases are useful for replacing a command, but what if we want to make an alias that involves a variable of some kind, like a path or file or some string of text? For that, we can use a function instead of an alias. This gets a little bit deeper into Bash scripting. To create a function, I'll write function greplog a set of parentheses, and a set of braces. Inside the braces, I'll write grep $1 /var/log/syslog; This function, called greplog, will take the first argument provided and use it as a term to search in the syslog with grep. So to run this, I'll write greplog dhcp and see all the lines from the log that match my term without having to type out my grep statement. That's a pretty basic example, but you can see how you can start to make your own handy shortcuts without making a whole Bash script. These aliases and functions will exist in this particular Bash session. If I closed my shell window here and reopened it, they'd be gone. In order to make them persist, I need to add them to my Bashrc file. To do that, I'll open up the file, go down to the bottom, and define them here. I'll save that and close it, and then I'll close and reopen my terminal. There's my alias and my function, ready to use in a new terminal. It's not uncommon to build up a few of these over time as you find ways to save yourself a little bit of work.

Contents