The first day of Shell learning

Source: Internet
Author: User
Tags arithmetic

Shell Basics

Shell's Make

is the command that interprets the user, the user enters a command, and the shell interprets the execution of a rule, which is called Interactive (Interactive),

The shell also has a way of executing a command called batch, in which the user writes a shell script, which has many commands that allow the shell to complete these commands at once without having to hit the command one at a time.

There are many shells in history (e.g. SH,CSH,KSH,TCSH) and bash.

Shell Execution Script

#!/bin/bashecho "This is a test" CD: Ls

Execution Result:

650) this.width=650; "style=" width:450px;height:150px; "src=" http://s4.51cto.com/wyfs02/M02/85/9B/ Wkiol1ep202ipkvjaaaobnz2eq8485.png-wh_500x0-wm_3-wmp_4-s_1783780033.png "title=" 4.png "border=" 0 "hspace=" 0 " Vspace= "0" width= "height=" alt= "Wkiol1ep202ipkvjaaaobnz2eq8485.png-wh_50"/>

The shell script uses # notation, which corresponds to the C//comment. However, if # is at the beginning of the first line and is the exception, it indicates that the script uses the interpreter specified later to interpret the/bin/sh.

Execution mode: chmod +x exe.sh

The shell will fork a child process and call exec to execute it./script.sh this program, the EXEC system call should replace the code snippet of the process with the code snippet of the./script.sh program and execute it from its _start. However, script.sh is a text file, there is no code snippet and _start function, how to do? In fact, Exec has another mechanism, if you want to execute a text file, and the line with the Shebang (#) to specify the interpreter, then use the code snippet of the interpreter program to replace the current process, and executes from the _start of the interpreter, and this text is passed to the interpreter as a command-line argument. Therefore, executing the above script is equivalent to executing the program.

Shell variables

Environment variables can be passed from the parent process to the child process, so the environment variables of the shell process can be passed from the current shell process to the forked child process. Use the PRINTENV command to display the environment variables for the current shell process.
Local variables exist only in the current shell process, with the SET command to display all variables defined in the current shell process, including local variables
and environment variables) and functions.
Environment variables are concepts that are present in any process, and local variables are shell-specific concepts.

Varname=value

Note that there can be no spaces on either side of the equal sign, or it will be interpreted by the shell as command and command line arguments

Variable reference

If a variable is called VARNAME, use ${varname} to represent its value

echo $SHELLecho $SHELLabecho ${shell}echo ${SHELL}ABC

Note that when you define a variable without $, the value of the variable to use $, and the C language is different, the shell variable does not need to explicitly define the type, in fact the shell variable value is a string, such as we define VAR=45, in fact, the value of Var is a string 45 instead of an integer. The shell variable does not need to be defined before it is used, and the value is an empty string if a variable is not defined.

Command substitution: ' or $ ()

It is also a command that is enclosed in anti-quotes, which the shell executes first, and then immediately substitutions the output to the current command line.

$ Date= ' Date ' $ echo $DATE $ date=$ (date) $ echo $DATE

650) this.width=650; "style=" width:200px;height:34px; "src=" http://s2.51cto.com/wyfs02/M01/85/9C/ Wkiom1ep6lzskepwaaanmebpv4a306.png-wh_500x0-wm_3-wmp_4-s_3093022395.png "title=" 5.png "border=" 0 "hspace=" 0 " Vspace= "0" width= "height=" alt= "Wkiom1ep6lzskepwaaanmebpv4a306.png-wh_50"/>

Arithmetic substitution: $ (()) (for arithmetic calculations, the value of the shell variable in $ (()) is converted to integers. Note : $ (()) can only be used with the +-*/and () operators, and can only do integer operations )

$ var=45//above refers to the VAR bit before initialization is the string $ echo $ (($VAR +3))

Operation Result: 48

Escape character

If you create a file with $ $ (with spaces in the middle)

$ Touch \$\ \$

Transfer character \: The first for the symbol $, the second for the space, and a third for the $

Single quotation marks

Single quotation marks are used to hold the literal value of all characters within the quotation marks, even if the \ and carriage returns within the quotation marks are no exception, but single quotes cannot appear in the string

Echo ' Date '

Double quotes

Double quotation marks are used to keep the literal value of all the characters in the quotation marks (carriage return is no exception), except for the following example ( meaning to interpret )

$ plus variable name to take the value of the variable
Anti-quotation marks still indicate command substitution
\$ represents the literal value of $
The literal value of \ ' represented ' (anti-quote)
The literal value of \ "represents"
\ \ denotes the literal value of \ In addition to the above, there is no special meaning in front of other characters, only the literal value:

echo "' Date '"

650) this.width=650; "style=" WIDTH:200PX;HEIGHT:27PX; "src=" http://s1.51cto.com/wyfs02/M02/85/9C/ Wkiol1ep8r7tbpbdaaain8ubzne633.png-wh_500x0-wm_3-wmp_4-s_2331963397.png "title=" 56.png "border=" 0 "hspace=" 0 " Vspace= "0" width= "height=" alt= "Wkiol1ep8r7tbpbdaaain8ubzne633.png-wh_50"/>

Summary: The result can be seen double quotation marks have the function of interpretation, single quotation marks do not explain, just output content.

The first day of Shell learning

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.