Shell for Linux Learning

Source: Internet
Author: User
Tags echo command

What is shell? Shell is an interface for user interaction with the kernel. It can be used as a command interpreter. After a user inputs a command, shell will explain it and then send it to the kernel for execution,


After knowing what shell is, let me know which shell is used in Linux. Isn't there more than one shell in Linux? Of course, you can view the shell of the current system by viewing the/etc/shells file.

The differences between various shells are similar. You only need to know one of them. In most Linux systems, the default shell is Bash,

Let's get started with Bash:


1. built-in bash shell commands:

With type, we can know whether an imperative built-in command is an external command.

Type [-T] command

-T: When the parameter T is added, the result is as follows:

File: indicates an external command.

Alias: indicates the name set for this imperative alias.

Builtin: indicates that this command is a bash built-in command.



2. Display and setting of bash Variables

Echo, unset

You can use the echo command to display the variable. However, when the variable is displayed, you must add "$" to get the path content.

Echo $ path or ECHO $ {path}

How to Set a variable:

The following are some rules:

The a variable and the variable content are connected with a = sign.

No space is allowed on both sides of the equal sign B. If a space is displayed, single quotation marks or double quotation marks are required.

Special characters in double quotation marks C. For example, $ can retain the original content, but the single quotation marks cannot

D. If other commands are required to provide information in a string of commands, you can use the 'command' or '$' (command)

E. Cancel a variable and use unset



3. view environment variables

Use the Env or export command

View all the variables in the system




4. Read variables from the keyboard

This knowledge point is used in many shell scripts.

5. Declare the variable type

Declare [-iaxr] VaR

A: array type

I integer

X global variables

R read-only type

Have you seen the difference above?




The following describes some useful technologies.


Delete and replace variable content


Delete the variable content. Remember the following four symbols:

#: Minimum character matching the content to be deleted from the left of the variable content

#: The longest matching character from the left of the variable content to the content you want to delete

%: Minimum character matching the content you want to delete starting from the right of the variable content

%: The longest character that matches the content you want to delete from the left of the variable content



* Indicates 0 or multiple arbitrary characters.


Syntax for replacement:

$ {Var/old/new} // Replace the first one

$ {Var // old/new} // replace all



Modify bash logon information:

First use CAT/etc/issue


What should you do if you want all users to obtain some information after logging on, such as announcements?

Modify the/etc/motd File


Bash environment configuration file



The following two concepts are introduced:

Login Shell and non-Login Shell

Login Shell: Obtain bash requires a complete login process, called Login Shell, for example, from the tty1-tty6 login, need to enter the user account and password, at this time obtain bash become login-shell

Non-login shell: No Logon is performed when Bash is obtained.

When login shell is started, two configuration files are read back.

/Etc/profile: This is the overall setting of the system. You 'd better not change it.

~ /. Bash_profile or ~ /. Bash_login or ~ /. Profile belongs to the user's personal settings. Write your data here if you want to change it.

For the following three files, Login Shell settings will only read one of the above three minutes, and the read order will read the above


Since/etc/profile and ~ /. Bash_profile is read in the login shell, so you need to write your preference settings to the above file, usually log out, can I directly read the configuration file without logging out? Yes. Use the source command.


Source configuration file

Source ~ /. Bash_profile

Or .~ /. Bash_profile



Data Stream redirection


Data Stream redirection literally transfers data to other places. By default, we execute a command, regardless of its right or wrong, and the result is actually on the console, now we want to save it to a file. For more information, see the following:

Example 1

Set execution result to result.txt


Save the error information in the execution process to err. log.


A brief explanation of the above case:

Use the standard input code as 0 <or <

Use the standard output code 1> or>

Standard Error output: Code 2: Use 2> or 2>


> Indicates writing after clearing> indicates accumulating




Example 2 discard the error message and only display the correct information

Find/home-name. bashrc 2>/dev/null



Example 3 store the correct information and error information in different information


Find/home-name. bashrc> list_right 2> list_error


Standard Input

A simple description means that the data originally required by the keyboard is replaced by the file content.

Example 1 use the cat command to create a file

Cat> catfile


Cat file test

Press Ctrl + D to end

Then use Cat catfile to view the content

Example 2


Pipeline command

The pipeline command is "|". The so-called pipeline is to use the result of one command as the input of another command.

Example 1


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: 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.