Bash is that tool in *NIX-land that you’re really only thankful for when you need it. Of course, most people don’t realize it, but that’s most of the time. So many things in *NIX-land depend upon bash. The command line is the biggie. But you wouldn’t be pulling off fantastic shell scripting if it weren’t for bash. So I thought it apt (pun intended) to offer these tips to make your bash life all the easier.
1: Use aliases
An alias is a shortcut for a command that’s challenging to type (or to remember). Instead of typing sudo apt-get update and sudo apt-get upgrade, you could add the alias alias update=”sudo apt-get update ; apt-get upgrade” to your ~/.bashrc file. Then, you’d just have to issue the command update to do the work. Aliases are simple ways to keep the typing in the command line down to a minimum, as well as saving you the trouble of remembering tricky commands.
2: Use bash history
This one is so fundamental that people often forget it’s even there. Bash retains a history of all commands issued. Typically, bash will retain the last commands run (how many bash retains will depend upon your distribution). That’s a lot of commands to remember. But how do you see them? You can issue the command history, which will list out all of the commands used (currently mine lists out 596 commands). What you might not know is that you can use the up and down arrows to scroll through the history listing. So instead of retyping that command you recently used, just hit the up arrow until you find the command. And unlike Windows, bash will retain this history even after you have logged out and logged back in.
3: Pipe output
One of the bash tricks I use daily is piping output of one command through another. This means that the output of one command will be used by the next command. The simplest example of this is using the dmesg command. When you issue dmesg, the output flies by so quickly you can’t see it. Instead, pipe the output to the less command so you can see the contents of dmesg one page at a time. This is done like so: dmesg | less. The “|” character tells bash to pipe the output of the first command through the second command.
4: Join commands
Instead of running ./configure, make, and make install separately (having to wait for the output of one command to finish before issuing the next), you can do it all from one command using the “;” character. The command will look like this:
./configure ; make ; make install
Of course, since running make install most often requires root privileges, you’ll have to run all three with sudo or after issuing the su command.
5: Outfox those spaces
This one will often bite you in the keister. We all know that Windows (and Windows users) are, for some strange reason, in love with using spaces in file/folder names. Why this is, I have NO IDEA. Linux (and all UNIX-based OSes) do not like spaces in file/folder names. But that’s okay. You can get around this by surrounding the full file/folder name with “”. So if you have to delete My Folder, you could do so with the command:
rm -rf “My Folder”
You could also use the “\” character instead, like so:
rm -rf My\ Folder
Notice the placement of the space in the My Folder name. That is important.
These five easy tips should make your bash life a wee bit easier. If you’ve dealt with any flavor of UNIX, you know that there are tons of such tips out there. I have found, however, that these five are the best ones to learn right away. They will save you a lot of time and prevent a lot of headaches. Do you have a handy bash tip to share with your fellow TechRepublic readers?
Check out Five Tips… the newsletter
Get a concise roundup of solutions and techniques that will make your IT job go more smoothly. TechRepublic’s Five Tips newsletter, delivered every Tuesday, gives you instant access to the information you need. Automatically sign up today.