Shell basic Syntax and commands

Source: Internet
Author: User
Tags arithmetic shebang

Execute script

Write a simple script test.sh:

#! /bin/shCD. LS

The shell script uses # notation, which corresponds to the C//comment. However, if # is at the beginning of the first line and is #! (called shebang), the exception is that the script uses the interpreter specified later/bin/sh interpret execution. If you add this script file with executable permissions and then execute:

chmod A +x test.sh./test.sh

The shell will fork a child process and call exec to execute it./test.sh this program, the EXEC system call should replace the code snippet of the process with the code snippet of the./test.sh program and execute it from its _start. However, test.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 first line specifies the interpreter with Shebang, then replace the current process with the code snippet of the interpreter program and execute it from the _start of the interpreter, and the text file is passed to the interpreter as a command-line argument. Therefore, executing the above script is equivalent to executing the program

$/bin/sh./test.sh

Executing in this manner does not require the test.sh file to have executable permissions.

If you enclose commands entered under the command line in parentheses, you will also fork out a sub-shell to execute the command in parentheses, where you can enter multiple commands separated by semicolons, such as:

$ (CD.; LS-L)

The effect of executing shell scripts with the above two methods is the same, CD: Command changes the PWD of the child shell without affecting the interactive shell. However the order

$ CD.; Ls-l

Then there are different effects on the CD. Commands are executed directly under the interactive shell, changing the pwd of the interactive shell, but this is equivalent to executing the shell script:

$ source./test.sh

Or

$ . ./test.sh

The source or. command is the shell's built-in command, which does not create a child shell, but rather executes the commands in the script line-by-row under the interactive shell.

Basic syntax variables

As a rule, shell variables consist of all caps and underscores, and there are two types of shell variables:

1. Environment 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.

2. Local Variables

exists only in the current shell process, with the SET command to display all variables (including local variables and environment variables) and functions defined in the current shell process.

Environment variables are concepts that are present in any process, and local variables are shell-specific concepts. In the shell, environment variables and local variables are defined and used similarly. Define or assign a variable to a shell:

itcast$ 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.

A variable is defined only in the current shell process, it is a local variable, the export command can be exported to the local variables as environment variables, defining and exporting environment variables can usually be completed in one step:

itcast$ Export Varname=value

It can also be done in two steps:

itcast$ varname=valueitcast$ export VARNAME

You can use the unset command to delete a defined environment variable or local variable.

itcast$ unset VARNAME

If a variable is called VARNAME, it can be represented by ${varname}, and its value can be expressed in $varname without ambiguity. The differences between the two representations are compared by the following example:

itcast$ Echo $SHELL

Note that when you define a variable, you do not use $ when you take the variable value. Unlike the C language, shell variables do not need to be explicitly defined types, in fact the values of shell variables are strings, 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.

File name Substitution (Globbing): *? []

The characters used for matching are called wildcards (Wildcard), as follows:

Wildcard *   matches 0 or more arbitrary characters?   Matches an arbitrary character [several characters]  matches one occurrence of any character in a square bracket
$ ls/dev/ttys*$ ls ch0?. doc$ ls ch0[0-2].doc$ ls ch[012]   [0-9].doc 

Note that the file name that globbing matches is expanded by the shell, which means that the parameter has been expanded before it is passed to the program, such as the LS ch0[012].doc command above, If the current directory has Ch00.doc and Ch02.doc, the parameters passed to the LS command are actually the two file names instead of a matching string.

Command substitution: ' or $ ()

It is also a command that is enclosed in quotation marks, and the shell executes the command first, and then the output is immediately substituted to the current command line. For example, define a variable to hold the output of the date command:

itcast$ date=' DATE ' itcast$ echo $DATE

Command substitution can also be expressed in $ ():

itcast$ date=$ (DATE)
Arithmetic substitution: $ (())

For arithmetic calculations, the value of the shell variable in $ (()) is converted to an integer, the same meaning as the $[] equivalent for example:

itcast$ var=45itcast$ echo $ (($VAR +3)) $ (()) can only be used with the +-*/ and () operators, and can only do integer operations. $[base#n], where base represents the binary, n is interpreted as base, followed by an operand, interpreted in decimal. echo $[2#10+11]echo $[8#10+11]echo $[10#10+11]   
Escape character \

Similar to the C language, \ is used as an escape character in the shell to remove the special meaning of a single character immediately following it (except for a carriage return), in other words, the literal value of the character immediately followed. For example:

itcast$ echo $SHELL/bin/bashitcast$ echo \ $SHELL $shellitcast$ echo \ \

For example, create a file named "$ $" to do this:

itcast$ Touch \$\ \$

There is also a character that does not have a special meaning, but it is cumbersome to use it as a filename. This is not possible if you want to create a file with a filename that begins with the-number:

itcast$ Touch-hellotouch:invalid option- htry ' touch--help ' for more information. 
Even adding \ Escaping is an error:
itcast$ Touch \-hellotouch:invalid Option-- htry ' touch--help ' for more information. 

Because the various UNIX commands take the command line arguments that begin with the-number as an option for the command, not as a file name. If you do not want to process a file name that begins with a-number, there are two ways to do this:

itcast$ Touch./-hello

Or

itcast$ Touch---Hello

\ There is also a use, after \ hit Enter to indicate the continuation of the line, the shell will not immediately execute the command, but instead of moving the cursor to the next line, a continuation prompt, waiting for the user to continue to input, and finally all the continuation of the line as a command to execute. For example:

itcast$ ls >-L (Output of ls-l command)
Single quotation marks

Unlike the C language, the single and double quotes in a shell script are the same as the delimiter of the string (described in the next section of the double quotation mark), not the delimiter of the character. 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. If the quotation marks are entered without pairing, the shell gives a continuation prompt that asks the user to match the quotation marks. For example:

itcast$ echo ' $SHELL '$SHELLitcast $ Echo ' abc\ (carriage return) > DE ' (press ENTER again to end the command)ABCDE 
Double quotes

The content enclosed in double quotes is treated as a single string. It prevents wildcard expansion, but allows variable expansion. This is different from the way single quotes are handled

itcast$ date=$ (DATE) itcast$ echo "$DATE"itcast$ Echo ' $DATE ' 

Shell basic Syntax and commands

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.