The GNU Bourne-Again SHell (BASH), as the name implies, is an extension of the Bourne shell. It provides a Command Line Interface (CLI) for working on *nix systems and is the most common shell used on Linux systems. Chances are that if you’re managing your own virtual or dedicated server – you’ll end up using the
bash shell fairly often. The
bash shell contains many features and enhancements that were not present in the Bourne shell – and has such has a very flexible and powerful programming interface, as well as a user-friendly command interface. As we kick off our tutorial series regarding the management of linux servers – we’ll touch base on some of the unique features of the
bash shell, along with some of its more commonly used functions.
Often times when you enter commands in
bash the complete text of the command is not necessary in order for the shell to understand what it is you want it to do. For example, let’s say we have the following directory structure:
If you wanted to change directories from the current one to files, for example, you would use the following command:
While this command will work,
bash allows you to accomplish the same exact thing in an even easier way. Since the files folder is the only thing in the directory that begins with the letter f,
bash is able to figure out what you want to do after you simple type:
Keep in mind that whenever you want bash to finish a command for you – you’ll need to hit the Tab key at the end of your command. When you do this
bash finishes the command for you and displays it on the screen. The command won’t actually execute until you press the Enter key to verify that the command
bash came up with is correct.
Furthermore if there is more than one file/folder in the working directory that starts on the same letter, for example let’s say we make a new folder and call it food, in order to still easily access the files folder we could need to tell
If you enter a command, hit Tab and
bash beeps at you – it means that it does not have enough information to complete your command. After beeping,
bash will leave the command on the screen as it was entered – thus enabling you to enter more information without needing to retype what was already typed.
Another great feature of bash that makes typing commands even easier is by enabling users to use wildcards within their commands. The
bash shell supports 3 different kinds of wildcards:
- * matches any character and any number of characters
- ? matches any single character
- […] matches any single characters contained within the brackets.
Wildcards work much in the same way the command line completion functions. Let’s use the same directory structure we were working with before:
Now let’s say we want to change over to our food directory. We already know about 2 different ways we can do that – and now wildcards gives us a 3rd solution. We can use the following command:
The * matches any character and any number of characters, so the shell will replace
fo* with food, since that’s the only file in the directory that matches the requirements. However what would have happened if we simply used
cd f*? If more than one file in the directory starts off with the same letter(s) – the shell will try to replace the
f* with the list of filenames in the directory that match the wildcard requirements, it will then jump over the the directory that is alphabetically first on the list (which in our case would’ve been files, when we really wanted food).
Now let’s add a few files to our directory structure, turning it into the following:
Now let’s say we want to list all of the files that have a .html extension. This can be easily accomplished by using:
In this case
bash would replace the *.html with the names of all the files in the directory that match the requirement. So the new command that would be displayed would be:
lpr about.html contact.html index.html serivces.html
Keep in mind that with the * wildcard you could have also used any of the following:
The ? wildcard is used in the same exact way as the * wildcard is except that the ? wildcard will only match a single character. Let’s add a few more files to our directory structure:
Now let’s say we want to list all of our service files, we could use the ? wildcard to accomplish this by telling
Furthermore we could also use the […] wildcard to generate the same list. Provided we know we have 3 service html files – we would tel
lpr service.html or specify a range via