Shell Programming: Basic Syntax

Source: Internet
Author: User
Tags arithmetic

To master a language, you must first master its grammar. As in C, the shell has its own syntax.

Variable

As a rule, shell variables usually begin with an underscore, consisting of letters, numbers, and underscores of any length. 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.

    1. 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=value

itcast$ Export VARNAME

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

itcast$ unset VARNAME

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), such as: *? [] specific as follows:

    * Match 0 one or more arbitrary characters

    ? match an arbitrary character

[ several characters ] matches one occurrence of any character in a square bracket

itcast$ ls/dev/ttys*

itcast$ ls ch0?. Doc

itcast$ ls Ch0[0-2].doc

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

It is also a command enclosed by the "'" backslash (below the ESC key), the shell executes the command first, and 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)

You can also directly echo ' date ' but not wrap it, that is, after performing echo ' Ls-alh ', the continuous output does not break the line.

Arithmetic substitution

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

itcast$ var=45

itcast$ Echo $ (($VAR +3)) equivalent to Echo $[var+3] or $[$VAR +3]

only the +-*/and () operators can be used in $ (()), and only integer operations can be done .

$[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 $[16#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/bash

itcast$ echo \ $SHELL

$SHELL

itcast$ echo \

For example, create a file named "$ $" (with a space between them) 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. If you want to create a file with a filename that begins with the-number, this is incorrect:

itcast$ Touch-hello

Touch:invalid option--H

Try ' Touch--help ' for more information.

Even adding \ Escaping is an error:

itcast$ Touch \-hello

Touch:invalid option--H

Try ' 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

( ls-l output of the command)

Single quotation marks

Unlike the C language, single quotes and double quotes in shell scripts are the same as the delimiter of the string (described in the next section of the double quotation mark), not the character's qualifier. 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 '

$SHELL

itcast$ Echo ' abc\ (carriage return)

> DE ' (press ENTER again to end the command)

Abc

DE

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

date=$ (date)

echo "$DATE"

Echo ' $DATE '

Another example:

itcast$ var=200

itcast$ Echo $VAR

200

itcast$ Echo ' $VAR '

$VAR

itcast$ echo "$VAR"

200

Shell Programming: Basic Syntax

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.