First knowledge of the shell

Source: Internet
Author: User
Tags echo command

What is the shell?

Believe that as long as touch the computer, for the operating system (whether Linux, Unix or Windows) a little concept of friends have heard the term, because as long as there is an operating system, it is inseparable from the Shell of this thing. The management of Linux often need to go online, while online text interface transmission speed must be relatively fast, and less prone to disconnection or information outflow problem. Managing the entire computer hardware is actually the core of the operating system (kernel), a core that needs to be protected. So we generally use the shell to communicate with the core, so that the core can achieve the work we want to achieve.

To be popular: The shell is the interface tool responsible for communicating with the core kernel of the system, reaching the user's desired results by communicating with the core.

The Shell under Linux

In the early years of the Unix era, there were many development, so because the shell is based on the development of different versions, such as often heard Bourne shell (SH), the default C shell in Sun, the commercially used K shell, and TCSH, and so on, each A SHELL has its own characteristics. As for Linux, this version is called the "Bourne Again Shell (bash)", which is an enhanced version of the Bourne shell and is developed under the GNU architecture.

Cat/etc/shells--View the shell that exists in the system
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/zsh

/bin/bash is the default shell for Linux, so why is the legal shell on our system writing to/etc/shells this file? This is because some of the services in the system are running to check the shells that the user can use, and these shell queries are based on the/etc/shells file. Bash is one of the most important tool software in the GNU program and is currently the standard shell for Linux distributions. Bash is primarily compatible with SH, and the shell version is enhanced based on the needs of some users. The main advantages of bash are the following:

Historical History of command

We can find the previous or subsequent input commands by pressing the "up and Down" button in the command column, and the default command memory function can reach 1000. In other words, the commands you have entered are almost always recorded. So many command logs are saved in the. bash_history file in your home directory. Note that the ~/.bash_history record is the previous login run before the command, and as for this time the login Run command is cached in memory, when you successfully log off the system, the command memory will be recorded in the. bash_history.

TAB command completion

If I want to know how many commands I can run in my environment, you can display all the commands you can run by pressing the [TAB] button two times straight after the cue character of bash. What if you want to know all the commands in the system that start with C? Just press "C[tab][tab".

To alias an order

If I need to know all the files under this directory (including hidden files) and all the file attributes, then I have to release "Ls-al" such as the command, whether I feel trouble, there is no faster replacement way? Then you need to use the command alias. Like alias La= ' Ls–al ', running LA is equivalent to executing the ls–al command.

Job Management Job CONTROL,FG,BG

The use of pre-and post-and-back controls allows for smoother work. As for job control (jobs), it's a broader use that allows us to throw work into the background at any time without worrying about using [Ctrl] + C to stop the program. In addition, it is possible to achieve multi-tasking in a single sign-on environment.

Programmatic Scripting shell scripts

The shell scripts under Linux can write a file of successive commands that your usual management system needs to release, which can be used to detect the host in interactive ways, and can be designed based on the environment variables and related commands provided by the shell. Something that used to be written in a DOS-and-Don language, using a simple shell scripts underneath Linux can help you achieve it.

Wildcard characters

In addition to the complete string, bash supports a number of wildcard characters to help users query and execute commands. For example, if you want to know how many files at the bottom of/usr/bin are using "Ls-l/usr/bin/x*" as a file with the beginning of X.

Shell variable function

Variables are a very important feature in the bash environment, and we know that Linux is a multi-user multitasking environment where each user can get a bash, and everyone can use bash to release a command to achieve their intended purpose, but the question is how bash learns the difference between the same command execution The result? This requires a "variable".

You can use the echo command to take a variable, but when the variable is taken, it must be preceded by a "$" line, for example, to know the contents of PATH, echo $PATH or Echo ${path} is required.

Create a custom variable echo $mypath, if executed, the result is empty, execute Mypath=/usr/bin/mypath, then echo $mypath, the =/usr/bin/mypath will appear. The variable is assigned by equals =. Here's a list of common configuration rules:

    1. Variables and variable contents are linked by an equal sign "=";
    2. The equal sign can not be directly connected to the space character, such as "mypath=/usr/bin/mypath" or "Mypath=/usr/bin/mypath" as the error;
    3. Variable names can only be English letters and numbers with an underscore, the beginning character cannot be a number;
    4. Variable content If there is a space can use double quotation marks "" "or single quotation mark" ' "To reference the variable content, but the double quotation marks in the special word such as $, etc., you can retain the original characteristics, such as" var= "Lang is $LANG" "Then" echo $var "can get" Lang is en_US " The special characters in single quotation marks are only ordinary characters (plain text), such as "var= ' Lang is $LANG '" then "echo $var" can get "Lang is $LANG";
    5. You can use the caret character "\" To turn special symbols (such as [Enter], $, \, space, ' etc.) into general characters;
    6. In a string of commands, you also need to reference the information provided by other commands, using the inverted single quotation mark "' Command '" or "$ (command)". If you want to obtain the system core version information "version=$ (UNAME-R)" and "Echo $version" can be "2.6.18-128.el5";
    7. If the variable is the content of the amplified variable, the contents can be summed up with "$ variable name" or ${variable}, such as "Path=" $PATH ":/usr/bin";
    8. If the variable needs to run in another subroutine, you need to export the variable to the Global environment variable: "Export PATH", when the global variable does not take effect in time and requires bash
    9. Usually uppercase characters are system default variables, self-configuring variables can use lowercase characters, easy to judge;
    10. The method for canceling a variable is available unset: "unset variable name". For example, remove the MyPath configuration: "Unset mypath".

Use env to view the system's environment variables and their descriptions: env is a shorthand for environment (environment), which can be used to list all the environment variables that exist in the system, and export can implement this function. The following are some common environment variables:

    • HOME
      Represents the user's home directory. We can use CDs to go to our home directory, or we can simply go back to the user's home directory using a CD.
    • SHELL
      Tell us, which program is the shell that this environment uses now? Linux defaults to using/bin/bash.
    • Histsize
      This has to do with the history command, and the commands we have issued can be recorded by the system, and the number of records is configured by this value.
    • MAIL
      When we use mail this command when the message is received, the system will go to read the Mail box file (mailbox).
    • PATH
      Run the path of the file search, directory and directory in the middle with a colon (:) delimited, because the file search is sequentially queried by the directory within the PATH variable, so the order of the directory is also very important.
    • Lang
      Language encoding data, when we start some Perl program language files, he will actively analyze the language data file, if found that he could not parse the encoding language, may produce errors. Generally speaking, our Chinese encoding is usually zh_cn. UTF-8.
    • RANDOM
      Random random number of variables, most of the current distribution will have a random number generator, that is/dev/random this file. We can randomly get random values through this random number file-related variable ($RANDOM). In BASH environment, the contents of this random variable are between 0~32767, so if you just echo $RANDOM, the system will randomly take out a value between 0~32767. If I want to use a value between 0~9, you can use declare to specify a numeric type to implement.

Use set to view all variables (with environment variables and custom variables)

Bash is not just an environment variable oh, there are also some variables related to the Bash interface, as well as the user's own defined variables exist. The set command, in addition to the realistic environment variables, displays all the other variables within bash. Here are some of the following:

[[Email protected] ~]# set

Bash=/bin/bash <== Bash's main program placement path

bash_versinfo= ([0]= "4" [1]= "1" [2]= "2" [3]= "1" [4]= "Release" [5]= "X86_64-redhat-linux-gnu") Version of <== Bash

Bash_version= ' 4.1.2 (1)-release ' <== is also a bash version

Colors=/etc/dir_colors <== The color recording file used

Columns=80 <== in the current terminal environment, the field used has several character lengths

Histfile=/root/.bash_history <== History Command records the placement file

histfilesize=1000 the maximum number of <== history commands recorded

histsize=1000 <== Current environment, the maximum number of records that can be recorded in a history command

Home=/root <== Home Directory
hostname=dyqd2011 <== Host Name
HOSTTYPE=X86_64 <== Host installed software main type

ifs=$ ' \t\n ' <== default delimiter

lines=24 <== The maximum number of rows currently under the terminal

MACHTYPE=X86_64-REDHAT-LINUX-GNU <== installed machine type

Mailcheck=60 <== is related to mail. Every 60 seconds to retrieve the mailbox, there's no new information.

Oldpwd=/home <== last working directory. We can use the CD-to fetch this variable.

OSTYPE=LINUX-GNU types of <== operating systems

ppid=1981 PID of <== Parent Program

Ps1= ' [\[email protected]\h \w]\$ ' <== command prompt character, which is our common [[email protected] ~]# or [dmtsai ~]$ configuration value

$ <== The PID currently used by this shell

? <== the callback value of the command just finished running

Note: In the case of Linux default, variables configured with {uppercase letters} are generally required by the system.

Symbolic meaning associated with the PS1 hint character:

\d: Can display the "Day of the Week" date format, such as: "Mon Feb 2";

\h: Full host name;

\h: Takes only the name of the hostname before the first decimal point;

\ t: Displays the time as "HH:MM:SS" in the 24-hour format;

\ t: Displays the time as "HH:MM:SS" in the 12-hour format;

\a: Show time as "hh:mm" in 24-hour format;

\@: Display time for the "AM/PM" style in the 12-hour format;

\u: The current user's account name, such as "root";

\v:bash version information, such as Bird's Test motherboard is 3.2.25 (1), only take "3.2" display;

\w: The full working directory name, the directory name that is written by the root directory. But the home directory will be replaced by ~;

\w: Use the basename function to get the working directory name, so only the last directory name will be listed;

\#: The first few orders issued;

\$: Prompt character, if root, the prompt character is #, otherwise it is $.

Note: Inside bash? This variable represents "the value returned from the last run of the command", and when we run some commands, the commands return a running code. In general, if the command is run successfully, a value of 0 is returned, and the error code is returned if there is an error in the running process. Like echo $?

First knowledge of the shell

Related Article

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.