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.
Command-Line Completion
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:
- /news
- /bin
- /files
- .htaccess
- index.html
If you wanted to change directories from the current one to files, for example, you would use the following command: cd files
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: cd f<tab>
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 bash
to: cd fi<tab>
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.
Wildcards
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:
- /news
- /bin
- /files
- /food
- .htaccess
- index.html
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: cd fo*
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:
- /news
- /bin
- /files
- /food
- .htaccess
- about.html
- contact.html
- index.html
- services.html
Now let’s say we want to list all of the files that have a .html extension. This can be easily accomplished by using: lpr *.html
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:
lpr *html
lpr *htm
lpr *tml
lpr *ml
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:
- /news
- /bin
- /files
- /food
- .htaccess
- about.html
- contact.html
- index.html
- services.html
- service1.html
- service2.html
- service3.html
Now let’s say we want to list all of our service files, we could use the ? wildcard to accomplish this by telling bash
to: lpr service?.html
Furthermore we could also use the […] wildcard to generate the same list. Provided we know we have 3 service html files – we would tel bash
: lpr service[123].html
or specify a range via lpr service[1-3].html