Shell Learning Notes

Source: Internet
Author: User

The shell is born in Unix and is a tool for unix/linux interaction, and it doesn't make sense to learn the shell alone.

The shell needs to rely on other programs to do most of the work, which may be his flaw, but its undoubted strength is that the simple scripting language markup method is faster and more efficient than the program written in the C language.

The shell itself is a program written in C, it is a bridge for users to use Unix/linux, most of the user's work is done through the shell, the shell is both a command language, but also a programming language.

    1. As the command language, it interprets and executes the user-entered commands interactively.
    2. As a design language, it defines variables and parameters and provides a number of control structures that are available in high-level languages: loops and branches.

The shell has two ways of executing commands:

Interactive: Explains the execution of the user's commands, the user enters a command, and the shell interprets the execution of a single rule.

Batch processing: The user writes a shell script in advance, with a number of commands that allow the shell to execute the commands at once without having to hit the command one at a time.

There are many types of shells: bash, sh, ash, csh, Ksh, and so on, bash is the Linux standard Shell,bash fully compatible with SH.

There are two ways to run a shell script:

As an executable program./test.sh

As a parameter interpreter/bin/sh test.sh

Shell supports custom variables

Defining variables

Variable name does not add dollar sign ($) when defining a variable:

var iablename= "Value"

Note that there can be no spaces between the variable name and the equals sign, which may be different from any programming language you are familiar with.

The name of the variable is named according to the following rules:

    1. The first character must be a letter
    2. Cannot have spaces in the middle, you can use underscores (_)
    3. Punctuation cannot be used
    4. You can't use the keywords in bash

Using variables

Using a defined variable, simply precede the variable name with the dollar sign ($)

Your_name="Mazhiyuan"echo $your _nameecho ${your_name}

It is recommended to use curly braces for all variables; it's a good habit to become.

Variables that have already been defined can be redefined.

Read-only variables

Use the readonly command to define a variable as a read-only variable, and the value of a read-only variable cannot be modified.

Delete a variable

Use the unset command to delete a variable that cannot be used again after it has been deleted; The unset command cannot delete a read-only variable.

Variable type

When you run the shell, there are three different variables:

Local variables

Local variables are defined in a script or command, only valid in the current shell instance, and other shell-initiated programs cannot access local variables.

Environment variables

All programs, including shell-initiated programs, can access environment variables, some require environment variables to keep them running properly, and shell scripts can define environment variables when necessary.

Shell variables

Shell variables are special variables that are set by the shell program. Some of the shell variables are environment variables, some of which are local variables that guarantee the shell's normal operation.

Variable names can only contain letters, numbers, underscores, because some variables that contain other characters have special meanings, and such variables are called special variables.

Variable Meaning
$ File name of the current script
$n Arguments passed to a script or function, n is a number that represents the first parameter
$# The number of arguments passed to the script or function
$* All parameters passed to the script or function
[Email protected] All arguments passed to a script or function, when enclosed by double quotation marks (""), are slightly different from $*
$? Exit status of the last command or return value of the function
$$ The current shell process ID, which is the process ID for the shell script, which is where these scripts are located

Description

"$*" will all parameters as a whole, output all parameters in the form of "$ $ ... $n"

[email protected] will separate the parameters to output all parameters in the form "" "" $ "" "$n" "

Escape Character meaning
\\ Back slash
\a Alarm, Bell
\b BACKSPACE (delete key)
\f Page Break (FF), moving the current position to the beginning of the next page
\ n Line break
\ r Enter
\ t Horizontal tab (Tab key)
\v Vertical tab

Description

-E to replace the escape character

-e prohibit escape, default is not escaped

-n prohibits inserting line breaks

Command substitution means that the shell can execute commands first, save the output temporarily, and output it where appropriate.

' Command '

Note is the anti-quote

Variable substitution refers to changing the value of a variable based on its state (whether it is empty, whether it is defined, etc.).

Variable substitution forms that you can use:

form Description
${var} The variable's original value
${var:-word} If the variable var is empty or has been deleted (unset), then return to word, but do not change the value of var.
${var:=word} If the variable var is empty or has been deleted (unset), return to Word and set the value of Var to word.
${var:?message} If the variable var is empty or has been deleted (unset), then send message messages to the standard error output, which can be used to detect whether Var can be properly assigned.
If this substitution appears in the shell script, the script will stop running.
${var:+word} If the variable var is defined, it returns word but does not change the value of var.

Shell Learning Notes

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.