Chapter One Shell Basics

Source: Internet
Author: User

1.1 Introduction

The shell is a C language scripting language, it is the user and Linux bridge, the user input command to the shell processing, the shell will be the corresponding operation to the kernel (Kernel), the kernel to the results of processing output to the user.

Here is the processing flow:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/8B/5E/wKioL1hLhIqDimSkAACW4Y1j1qE949.png "title=" Image.png "alt=" Wkiol1hlhiqdimskaacw4y1j1qe949.png "/>

Since the shell is working on the Linux kernel, we also need to know about Linux.

Linux is a free trial and free-spread UNIX-like operating system, a POSIX and Unix-based multiuser, multitasking, multi-threaded and multi-CPU operating system.

Richard Stallman-Stallman, who launched the GNU program on September 27, 1983, aims to create a completely free operating system. To ensure that GNU software is free to use, reproduce, modify, and distribute, all GNU software has an agreement to grant all rights to anyone without any restrictions imposed by others, the GNU General Public License (GNU Plubic LICENSE,GPL), Plainly, it is impossible to do commercial use.

GNU is the recursive abbreviation for "GNU is not Unix". UNIX is the name of a widely used commercial operating system.

In 1085, Richard Stallman founded the Free Software Foundation (software FOUNDATION,FSF) to provide technical, legal and financial support for the GNU program.

In 1990, the GNU program developed major projects including Emacs (text editor), GCC (GUN Compiler Collection,gnu compiler Collection), bash, etc., and GCC was a set of GNU-developed programming language compilers. There are also libraries and tools for developing UNIX systems.

In 1991, Linuxs Torvalds (Linus-Torvalds) developed a UNIX-compatible Linux operating system kernel and released it under the GPL terms.

In 1992, Linux combined with other gun software, the completely free Gun/linux operating system was formally born, referred to as Linux.

In January 1995, Bob Young founded ACC, with Gnu/linux as the core, developed the Redhat Linux Business Edition.

The basic idea of Linux has two points: first, everything is a document; second, each software has a definite purpose. Very similar to UNIX thought.

1.2 Shell basically divided into two major categories

1.2.1 Graphical interface shell (GUI shell)

The GUI constructs a fully functional, simple and user-friendly desktop environment for UNIX or UNIX-like operating systems.

Mainstream desktop environment has kde,gnome and so on.

1.2.2 Command Line interface shell (CLI shell)

The CLI is an interface that types executable instructions at the user's prompt, and the user enters instructions through the keyboard to complete a series of operations.

The mainstream CLI implementation on Linux systems is bash, which is the default shell for many Linux distributions. There are also many shells used on Unix, such as TCSH, CSH, Ash, BSH, Ksh, and so on.

1.3 First shell Script

This tutorial focuses on the default bash Shell under most Linux distributions. Linux system is redhat under the CentOS operating system, completely free. Its commercial version of Rhel (Red Hat Enterprise Linux) comes from the same source code, unlike CentOS, which does not include closed source software and after-sales support.

Open test.sh with VI, write:

# vi Test.sh#!/bin/bashecho "Hello world!"

The first line sets the run environment, the second line prints Hello world!

Once written, there are three ways to execute a shell script:

Method 1: Execute # Bash Test.shhello world! with Bash interpreter Method 2: Add executable permissions # ll Test.sh-rw-r--r--. 1 root root 01:07 test.sh# chmod +x test.sh#./test.sh-bash:./test.sh:permission denied# chmod +x test. sh#./test.sh #/In the current directory Hello world! method 3:source command execution, with the current default shell execution [[email protected] ~]# source Test.shhello world!

1.4 Shell Variables

1.4.1 System Variables

At the command line prompt, execute export, env, or set to view system variables. Env is a display user environment variable, set is a shell variable display. The set variable can be imported into the env variable via export.

Some of the system variables that are commonly used when writing shell scripts:

$SHELL Default shell
$HOME Current User Home Directory
$IFS Default internal domain delimiter
$LANG Default language
$PATH Default executable program path
$PWD Current directory
$UID User ID
$USER Current user
$HISTSIZE History command size, can be set by Histtimeformat variable command execution time

1.4.2 Environment variables

The scope is the current shell process and its child processes, for example:

# a=1# Echo $a 1

Fails after exiting the shell.

1.4.3 Position Variable

A positional variable refers to the nth argument followed by a function or script.

$1-$n, it is important to note that starting with the 10th one is called with curly braces, for example ${10}

Shift controls the position variable, for example:

#!/bin/bashecho "1: $ shiftecho" 2: $ "Shiftecho" 3: $ "# bash test.sh a b c1:a2:c3:

Each time the shift command is executed, the number of positional variables is reduced by one, and the value of the variable is one ahead of the other. Shift N, which can be set to move forward n bits.

1.4.4 Special variables

$ The script's own name
$? Returns whether the previous command succeeded, 0 for execution, and not 0 for execution failure
$# Total number of positional parameters
$* All positional parameters are viewed as a string
[Email protected] Each positional parameter is considered a separate string
$$ Current Process PID

1.5 Variable references

1.5.1 Custom variables and references

# var=123# Echo $VAR 123

All variable references in the shell use the $ character followed by the variable name.

Sometimes an individual special character can affect a normal reference, so you need to use ${var}, for example:

# var=123# Echo $VAR 123# echo $VAR _ # Shell allows Var_ to be a variable name, so this reference considers this to be a valid variable name and therefore returns an empty # echo ${var}123

There are also times when the variable name and other strings are blocked, and it is mistaken for the entire variable:

# echo $VAR 456# echo ${var}456123456

1.5.2 The command result as a variable value

# var= ' echo 123 ' [[email protected] ~]# echo $VAR 123# var=$ (Echo 123) # echo $VAR 123

The inverse apostrophe here is equivalent to $ (), which is used to execute the shell command.


Blog Address: http://lizhenliang.blog.51cto.com

QQ Group: 323779636 (Shell/python devops Development Group)


1.6 Double and single quotation marks

When assigning a variable, the shell interprets the string following the space as a command if the value has a space:

# var=1 2 3-bash:2: Command not found# var= "1 2 3" # echo $VAR 1 2 3# var= ' 1 2 3 ' # echo $VAR 1 2 3

There is no difference, give another explanation:

# n=3# var= "1 2 $N" # echo $VAR 1 2 3# var= ' 1 2 $N ' # echo $VAR 1 2 $N

Single quotes tell the shell to ignore special characters, while double quotes explain the original meaning of the special symbol, such as $,!.

1.7 Notes

Shell annotations are also simple, as long as you add a # to each line, that means the shell ignores the explanation.


This article is from the "Li Zhenliang Technology Blog" blog, make sure to keep this source http://lizhenliang.blog.51cto.com/7876557/1881437

Chapter One Shell Basics

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.