Ultimate Guide for Using Bash Shell in Linux: Novice Approach

Sometimes, things just don’t work. What do you do if the GUI desktop stops responding to your mouse clicks? What if the GUI doesn’t start at all? You can still tell your Linux system what to do, but you have to do it by typing commands into a text screen. In these situations, you work with the shell , the Linux command interpreter. The bash shell, the default shell in most Linux distributions.

Using Bash Shell

If you’ve used MS-DOS, you may be familiar with COMMAND.COM, the DOS command interpreter. That program displays the infamous C:\> prompt. In Windows, you can see this prompt if you open a command window. (To open a command window in Microsoft Windows, choose Start>Run, type command in the text box, and then click OK.) Linux comes with a command interpreter that resembles COMMAND.COM in DOS, but it can do a whole lot more. The Linux command interpreter is called a shell.

The default shell in many Linux distributions is bash. When you open a terminal window or log in at a text console, the bash shell is what prompts you for commands. Then, when you type a command, the shell executes your command.

Just as there are multiple GUIs (GNOME or KDE) for Linux, you have a choice of shells besides bash. For example, some people prefer the C shell. You can easily change your default shell by using the chsh command.

In addition to the standard Linux commands, bash can execute any computer program. So you can type the name of an application (the name is usually more cryptic than what you see in GNOME or KDE menus) at the shell prompt, and the shell starts that application.

Understanding the syntax of shell commands Because a shell interprets what you type, knowing how the shell processes the text you enter is important. All shell commands have the following general format. (Some commands have no options.)

command [option1] [option2] . . . [optionN]

Issuing such a command is commonly referred to as a command line. On a command line, you enter a command, followed by zero or more options  (or arguments). These strings of options, the command-line options (or command-line arguments) modify the way the command works so that you can get it to do specific tasks.

The shell uses a blank space or a tab to distinguish between the command and options. This means you must use a space or a tab to separate the command from the options and the options from one another. If an option contains spaces, you put that option inside quotation marks. For example, to search for my name in the password file, I enter the following grep command (grep is used for searching for text in files):

grep “Emmett Dulaney” /etc/passwd

When grep prints the line with my name, it looks like this:

edulaney:x:1000:100:Emmett Dulaney:/home/edulaney:/bin/bash

If you create a user account with your username, type the grep command with your username as an argument to look for that username in the /etc/passwd file.

In the output from the grep command, you can see the name of the shell (/bin/bash) following the last colon (:). Because the bash shell is an executable file, it resides in the /bin directory; you must provide the full path to it.

The number of command-line options and their format depend on the actual command. Typically, these options look like -X, where X is a single character. For example, you can use the -l option with the ls command. The command lists the contents of a directory, and the option provides additional details. Here is a result of typing ls -l in a user’s home directory:

total 0
drwxr-xr-x 2 edulaney users 48 2010-09-08 21:11 bin
drwx------ 2 edulaney users 320 2010-09-08 21:16 Desktop
drwx------ 2 edulaney users 80 2010-09-08 21:11 Documents
drwxr-xr-x 2 edulaney users 80 2010-09-08 21:11 public_
html
drwxr-xr-x 2 edulaney users 464 2010-09-17 18:21 sdump

If a command is too long to fit on a single line, you can press the backslash key (\) followed by Enter. Then, continue typing the command on the next line. For example, type the following command. (Press Enter after each line.)

cat \
/etc/passwd

The cat command then displays the contents of the /etc/passwd file. You can concatenate (that is, string together) several shorter commands on a single line by separating the commands by semicolons (;). For example, the following command

cd; ls -l; pwd

changes the current directory to your home directory, lists the contents of that directory, and then shows the name of that directory.

Combining shell commands

You can combine simple shell commands to create a more sophisticated command. For example, suppose that you want to find out whether a device file named sbpcd resides in your system’s /dev directory because some documentation says you need that device file for your CD-ROM drive. You can use the ls /dev command to get a directory listing of the /dev directory and then browse through it to see whether that listing contains sbpcd.

Unfortunately, the /dev directory has a great many entries, so you may find it hard to find any item that has sbpcd in its name. You can, however, combine the ls command with grep and come up with a command line that does exactly what you want. Here’s that command line:

ls /dev | grep sbpcd

The shell sends the output of the ls command (the directory listing) to the grep command, which searches for the string sbpcd. That vertical bar (|) is known as a pipe because it acts as a conduit (think of a water pipe) between the two programs , the output of the first command is fed into the input of the second one.

Controlling command input and output

Most Linux commands have a common feature , they always read from the standard input (usually, the keyboard) and write to the standard output (usually, the screen). Error messages are sent to the standard error (usually to the screen as well). These three devices often are referred to as stdin, stdout, and stderr.

You can make a command get its input from a file and then send its output to another file. Just so you know, the highfalutin term for this feature is input and output redirection or I/O redirection. The following table shows the syntax of common I/O redirection commands.

Syntax of common I/O redirection commands.

0/Post a reply/Replies

Previous Post Next Post