Using Bash to Best Effect in Ubuntu

|

Basically, in the Bash environment, an administrator is working with text commands. An example of such a command is ls, which can be used to display a list of files in a directory. Bash has some useful features to make working with these line commands as easy as possible. Some shells offer the option to complete a command automatically. Bash has this feature, but it does more than just complete commands. Bash can complete almost everything: not just commands, but also file names and shell variables.


Using Automatic Command Completion
Using this feature is as simple as pressing the Tab key. For example, the cat line command is used to display the contents of an ASCII text file. The name of this file, which is in the current directory, is this_is_a_file. So, to open this file, the user can type cat thi and then press the Tab key. If the directory has only one file that starts with the letters t-h-i, Bash automatically completes the name of the file. If the directory has other files that start with the same letters, Bash will complete the name of the file as far as possible. For example, let’s say that there is a file in the current directory with the name this_is_a_text_file and another named thisAlsoIsAFile. Because both files start with the text this, Bash will complete only up to this and no further. To display a list of possibilities, you then press the Tab key again. This allows you to manually enter more information. Of course, you can then use the Tab key again to use the completion feature once more.

Working with the Tab key really makes the command line interface much easier. Imagine that you need to manage logical volumes on your server and you remember only that the command for that starts with lv. In this case, you can type lv and press the Tab key twice. The result will be a nice list of all commands that start with lv, from which you’ll probably recognize the command that you need.


Working with Variables
A variable is simply a common value that is used often enough by the shell that it is stored with a name. An example of such a variable is PATH, which stores a list of directories that should be searched when a user enters a command. To refer to the contents of a variable, prefix a $ sign before the name of the variable. For example, the command echo $PATH displays the content of the current search path that Bash is using.
On any Linux system, you’ll get quite a few variables automatically. For an overview of all of them, you can use the env (short for environment) command.

Normally, as a user, you’ll get your variables automatically when logging in to the system. The most important source of new variables is the /etc/profile file, a script that is processed for every user who logs in to the system. Want to add a new variable? Add it to the bottom of the /etc/profile file to make sure it is available for all users.


Working with Bash History
Another useful feature of the Bash shell is the history feature, which remembers and lets you reuse commands you have recently used. By default, the last 1,000 commands are remembered. This feature is useful for sessions beyond even the current one. A file, named .bash_history, is created in the home directory of every user, and this file records the last 1,000 commands that that user has entered. You can see an overview of these commands by typing history at the Bash prompt.

This is where the history feature becomes especially useful because you can reissue any command from this list without typing it all over again. If you want to run any of the listed (and numbered) commands again, simply type its number preceded by an exclamation mark. In this example, typing !5 would run aptitude show xen-source-2.6.16 again. In addition to the history command, you can also use the up/down arrow keys, page up/down keys, and Ctrl+p/Ctrl+n to browse the history.

A user can also erase his or her history by using the history command. The most important option offered by this Bash internal command is the option -c, which clears the history list for that user. This is especially useful because everything that a user types at the command line—such as passwords—is recorded. So use history -c to make sure your history is cleared if you’d rather not have others knowing what you’ve been up to. Once using this option, however, you can’t use the arrow up key to access previous commands, because those are all erased. Because everything you enter from the command line is saved, I recommend never typing a plain-text password in the first place, even if you regularly erase the history. The commands that do require you to enter a password will prompt you anyway if you don't enter one right away.

Source of Information : Apress Beginning Ubuntu Server Administration

0 comments:

Subscribe to Computing Tech

Enter your email address:

Delivered by FeedBurner

Add to Technorati Favorites Top Blogs