Bash! (1)

Source: Internet
Author: User

Bash

Preface
This article is translated from the bash chapter in slackware Linux unleashed (Third edition), but some necessary deletions are made, and some changes are made according to the actual situation, if necessary, refer to the original article. If you have any questions about this article, please contact me: con@nease.net

Introduction
This article will introduce in detail the most common shell and bash in Linux. Bash (Bourne again shell) is the default shell of Linux and is used by most users. In this article, you will learn:

What is shell?


The most common shell in Linux

Command-line completion and Configuration

Command history and alias

Redirection and MPs queue

Change Prompt

Job Control)

How to customize your BASH Shell

In addition, you will also see some of the most common bash commands and memory variable usage. After reading this article, you will be able to use Bash faster and more effectively.

Shells in a nutshell
What is shell? In short, this word seems to always appear in Linux, but many new users do not know its clear meaning. This section provides an exact explanation and explains why it is so important in Linux.

What is shell?
Shell is the interface between you (user) and Linux (or, more accurately, between you and Linux kernel ). Program . Each command you enter at the prompt is explained by the shell and then transmitted to the Linux kernel.
--------------------------------------------------------------------------------

Note: If you are familiar with M $-dos, you will recognize the relationship between DOS users and command. com. Wei
I. The difference is that command.com's functions are far from comparable to shell.

--------------------------------------------------------------------------------

Shell is a command-language interpreter ). Has its own built-in shell command set. In addition, shell can also be called by other effective Linux utilities and applications (utilities and application programs) in the system.

Whenever you type a command, it is interpreted by the Linux Shell. Some commands, such as printing the current working directory command (PWD), are included in Linux bash (like dos internal commands ). Other commands, such as COPY Command (CP) and move command (RM), are separate programs in a directory in the file system. For the user, you do not know (or may not care about) whether a command is built inside the shell or a separate program.

The figure shows how shell executes the command explanation. Shell first checks whether the command is an internal command. If not, it then checks whether the command is an application, here, the application can be a Linux utility, such as LS and RM, or a purchased commercial application, such as XV or public domain software ), like ghostview. Then shell tries to find these applications in the search path. The search path is a list of directories where executable programs can be found. If the command you typed is not an internal command and the executable file is not found in the path, an error message is displayed. If the command is successfully found, shell internal commands or applications will be decomposed into system calls and passed to the Linux kernel.

Another important feature of shell is that it is an interpreted programming language. The shell programming language supports the vast majority of program control structures, such as loops and functions, that can be seen in advanced languages, variables and arrays. The shell programming language is easy to learn, and once mastered, it will become your effective tool. Any command that can be typed at the prompt can also be put into an executable shell program, which means that a task can be executed simply and repeatedly in Shell language.

How to start Shell

Shell is started after you successfully log on to the system and always acts as a means of interaction between you and the system kernel until you exit the system. Each user on your system has a default shell. The default shell of each user is specified in the passwd file in the system. The path of this file is/etc/passwd. The passwd file also contains other things: the user ID of each person, a copy of the encrypted password, and the Program executed immediately after the user logs on (Note: To enhance security, currently, the system usually places the encrypted password in the shadow file, and the password stored in passwd is replaced by an X character) although it is not strictly specified that this program must be a Linux Shell, this is the case in most cases.


The most common Shell

In Linux and Unix systems, you can use a variety of different shells. The most common types are Bourne shell (SH), c shell (CSH), and Korn shell (Ksh ). All three shells have their advantages and disadvantages. The author of the Bourne shell is Steven Bourne. It is the original shell used by UNIX and can be used on every UNIX. Bourne shell is excellent in shell programming, but it is inferior to several other shells in dealing with user interactions.

C shell is written by Bill Joy, which considers the friendliness of the user interface. It supports features not supported by shell such as command-line completion. C shell is generally considered to be inferior to the Bourne shell programming interface, but C shell is used by many c programmers because the C shell syntax is very similar to the C language, which is also the origin of the C Shell Name.

The Korn shell (Ksh) is written by Dave Korn. It combines the advantages of C shell and Bourne shell and is fully compatible with the Bourne shell.

In addition to these shells, many other Shell programs absorb the advantages of these original shell programs and become a new shell. Common Linux systems include tcsh (CSH extension), Bourne again shell (Bash, SH extension), and public domain Korn shell (pdksh, KSh extension ). Bash is the default shell for most Linux systems.

The Bourne again shell





Bourne again shell (BASH), as its name implies, is an extension of the Bourne shell. Bash is fully backward compatible with the Bourne shell, and many features are added and enhanced based on the Bourne shell. Bash also contains many advantages in C and Korn shell. Bash has flexible and powerful programming interfaces and friendly user interfaces.

Why use Bash instead of sh? The biggest drawback of the Bourne shell is that it processes user input. It is very troublesome to type commands in the Bourne shell, especially when you type many similar commands. Bash has prepared several features to make it easier to input commands.

Command-line completion)

Generally, when you input a command in bash (or any other shell), you do not have to enter the full shell command to determine the command you want to input. For example, assume that the current working directory contains the following files and subdirectories:

News/bin/games/mail/samplefile test/
If you want to enter the test sub-directory, you will enter the following command:
CD Test
This command can meet your needs, but Bash also provides a slightly different method to accomplish the same thing. Because test is the only subdirectory in the current directory that starts with a letter T, bash can determine what you want to do after you only enter the letter T:
CD t
After you type the letter, the only possible difference is test. To end the command with Bash, press the tab key:

Cd t <tab>;
After you do this, Bash will help you complete the command and display it on the screen. However, the command is not executed until you press the Enter key. Bash will let you check whether the completed command is what you really need. When you enter a short command like this, you may not be able to see the value of the command. Even a short command may slow down the input speed, but when the command you want to enter is a little long, you will find out how beautiful this feature is.
But what happens when there is more than one file starting with a letter T in the directory? There is a problem when you use command completion. Let's take a look at the following situation. The current directory contains the following content:

News/bin/mail/samplefile test/tools/working/
Now there are two files starting with the letter T in this directory. If you still want to enter the test subdirectory, how can you use the command to complete it? If you type:
Cd t <tab>;
Bash will not know which subdirectory you want to enter, because the provided information is not unique. If you do, Bash will send a beep to remind you that you do not have enough information to complete your command. Bash does not change the input command after the beep, which enables you to enter more information based on the original, in this example, you only need to type another E and press the tab key. Bash has enough information to complete your command:
CD Test
Whenever you press the tab key, bash tries to complete the command as much as possible. If not, it will beep to remind you to need more information. You need to enter more characters, press the tab key again, and repeat the process until the command you expected appears.

Wildcard

Another easier way to make command input is to use wildcards in the command. Bash supports three wildcards:

[Copy to clipboard] [-]

Code:* Match any character and any number of characters

? Match any single character

[...] Match any single character contained in brackets


* The use of wildcards is supplemented by commands. For example, assume that the current directory contains the following files:

News/bin/games/mail/samplefile test/

If you want to enter the "test" directory, type "CD test" or use the following command to complete the Configuration:

Cd t <tab>;

Now there is a third way to do the same thing. Because only one file starts with a letter T, you can also use the * wildcard to enter the directory. Type the following command:

Cd t *

* Match any character and any number of characters, so shell will replace T * with test (the only file in the current directory that matches the configuration scheme ). The current directory contains only one file starting with a letter T, which is reliable. However, if more than one file in the current directory starts with a letter T, shell will try to enter the first matching Directory, which is the first directory in alphabetical order, this directory may not be what you expected.

A wildcard * is used to specify multiple files with similar names in the command to be executed. For example, assume that the current directory contains the following files:

Ch1.doc ch2.doc ch3.doc chimp config mail/test/tools/

If you need to print all files with the. Doc extension, you can use a simple command like this:

LPR *. Doc

In this example, bash replaces *. Doc with all files in the current directory that match the wildcard scheme. After Bash is replaced, the command is processed as follows:

LPR ch1.doc ch2.doc ch3.doc

The LPR command will be called with the ch1.doc, ch2.doc, and ch3.doc parameters.



--------------------------------------------------------------------------------

Note: In addition to the above examples, there are several methods:
LPR * Doc
LPR * oC
LPR * C
--------------------------------------------------------------------------------

Wildcard? Except for a single character, all other functions are the same as the wildcard? To print all files with the. Doc extension in the directory mentioned above, type the following command:

LPR ch ?. Doc

The wildcard [...] can match the characters or character ranges given in parentheses. The preceding directory is used as an example to print all files with A. Doc extension in the directory. You can type one of the following commands:

LPR ch3161232.16.doc

Or:

LPR ch[1-32.16.doc

Command history
Bash also supports command history? %

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.