Linux Shell Programming variable assignment and reference

Source: Internet
Author: User

we can use any kind of text editor, such as Gedit, Kedit, Emacs, VI, etc. to write shell scripts, which must start with the following line (must be placed in the first line of the file): #!/bin/sh... Note: It is best to use "!/bin/bash" instead of "!/bin/sh" if you use TC shell instead of tcsh, other similar. The symbol #! is used to tell the system which program executes the script, and this example uses/bin/sh. If you want to execute the script after the edit is finished and saved, you must first make it executable: chmod +x filenameThe script is then entered in the directory where the script is located./filename can be executed.  in shell programming, the use of variables without prior declaration, and the name of the variable names should follow the following rules: the first character must be a letter (a-z,a-z)cannot have spaces in the middle, you can use underscores (_)punctuation cannot be usedYou can't use the keywords in bash (you can see the reserved keywords with the help command)when you need to assign a value to a variable, you can write: variable name = valueto take the value of a variable, simply precede the variable name with a $ (note: When assigning a value to a variable, you cannot leave spaces on the "=" side) #!/bin/sh# Assign a value to a variable:a= "Hello World" #等号两边均不能有空格存在# Print the value of variable a:echo "A is:" $aChoose your favorite editor, enter the above, save as file first, then execute chmod +x first to make it executable, and finally enter./first executes the script. The output results are as follows: A Is:hello Worldsometimes variable names can be confused with other words, such as: num=2echo "The $numnd"The above script does not output "This is the 2nd" but "This is the", because the shell is going to search for the value of the variable numnd, but in fact the variable does not have a value at this time. At this point, we can use curly braces to tell the shell that the NUM variable is to be printed: num=2echo " This is the ${num}nd"The result is: the 2ndNote the position of the curly braces: num=2echo "This is the {$num}nd"The result of the output: This is the {2}ndIt is important to note that the default assignment for the shell is a string assignment. For example: var=1var= $var +1Echo $varthe print is not 2 but the same. There are several ways to achieve the effect we want: Let "Var+=1"var=$[$var +1]var= ' expr $var + 1 ' #注意加号两边的空格, otherwise the value is assigned as a string. Note: The first two methods are valid under bash and will be faulted under SH.  let represents mathematical operations, and expr is used for integer numeric operations, each of which is separated by a space, $[] The expression in parentheses is evaluated as a mathematical operation before the result is output.  Many of the variables in the shell scripts are automatically set by the system, and we'll explain them when we use them. In addition to the normal shell variables that are only valid within the script, there are environment variables, which are those that are processed by the Export keyword

Linux Shell Programming variable assignment and reference

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.