Shell Basics Tutorial--2. Variables and operators

Source: Internet
Author: User
Tags bitwise

2. Variables and operators

2.1. Types of variables

Local variables, environment variables, variable substitution (display variables), positional variables, standard variables, special variables;

2.2. Local Variables

Local variables are used in scripts in the user's current shell life cycle

On the command line, localtest= "test" set local variable, $ echo $LOCALTEST show local variable

The set command can see what local variables are in the Shell's life cycle

ReadOnly Localtest, you can set the Localtest to read-only local variables, and can not be assigned to the operation, at this time if the implementation of such as localtest= "local" will be an error. Note that variables set to ReadOnly are not possible to be re-writable, so use caution

ReadOnly, you can see how many read-only variables are in the system

2.3. Environment variables

Environment variables are used for all user processes (often as child processes). The logon process is called the parent process. User processes executed in the shell are called child processes. Unlike local variables (used only in the current shell), environment variables are available for all child processes, including editors, scripts, and apps.

Environment variables can be used both for parent and child processes, environment variables are included in local variables, but local variables that are not environment variables cannot run in all child processes

Set the user's environment variables in the $home/.bash_profile configuration file, and set the environment variables for all users in the/etc/profile configuration file.

Set environment variables with export chinapath= "China"

Use env or $ export to export information for all environment variables

You can also set the environment variable to read-only using a command like ReadOnly Chinapath

2.4. Variable substitution

Variable substitution is replacing its name with the value of the variable.

${variable_name} or $Variable _name, display the actual value to Variable_name

${variable_name:+value}, if Variable_name is set, value is displayed, otherwise null

${variable_name:?value}, if Variable_name is not set, displays the user-defined error message value

${variable_name:-value}, if Variable_name is not set, the value is displayed, and if it is set, the values of variable_name are displayed

${variable_name:=value}, if Variable_name is not set, the value is set to values, and the value of variable_name is displayed if it is set too

2.5. Variable cleanup

For example, Unset Chinapath, which clears the value of the variable Chinapath, displays a null value if the echo Chinapath is executed again

ReadOnly variables are not unset.

2.6. Position variables

Position variable Representation $0,$1,$1,$2,......,$9

$ $ $ $ $4 $ $6 $7 $8 $9
Script Name

Role 1: Use positional parameters in the script, such as script parm.sh

#!/bin/bash# Test Position variable echo "This is the name of the script: $" echo "This is the first positional parameter of the script:" Echo "two:" Echo "Three: $" echo "four: $4" echo "Five: $" echo "Six: $6" echo " Seven: $7 "echo" Eight: $8 "echo" Nine: $9 "

Execute command chmod u+x parm.sh, then execute./parm.sh A B C D E, output result

This is the name of the script:./parm.sh This is the first positional parameter of the script: a two: B Three: C Four: D five: E 6:7: 8:9:

Role 2: Pass parameters to system commands, such as

#!/bin/bash#parm_1.shfind/home/perfect/shell-name $1-print

Executes the command chmod u+x parm_1.sh, and then executes the./parm_1.sh myfile.txt, which is equivalent to the command find/home/perfect/shell-name Myfile.txt-print

2.7. Standard variables

Bash establishes some standard environment variables by default, which can be defined in/etc/profile.

such as Exinit, HOME, IFS, PATH, LOGNAME, MAIL, MailCheck ...

2.8. Special variables

$# the number of arguments passed to the script

$* displays all parameters passed to the script in a single string. Unlike positional variables, this option parameter can be more than 9

$$ the current ID number of the script run

$! Process ID of the last process running in the background

[email protected] Pass to the parameter list of the script, use quotation marks, and return each parameter in quotation marks

$-shows the current selection of shells used by the shell, same as the SET command function

$? Displays the exit status of the last command. 0 means no error, and any other job indicates an error

See an example

#!/bin/bash#parm_2.shecho "Display parameters: ¥#" echo "shows all parameters of the script: $*" echo "shows the process id:$$" echo "shows the previous command after the run state: $?"

Executes the command chmod u+x parm_2.sh, and then executes./parm_2.sh A China chinaitalb output results vary by machine

Number of display parameters: 3 Displays all parameters of the script: a China chinaitalb display process id:7799 display the previous command after the run state: 0

2.9. commands that affect variables

Declare is used to set or display variables. -F only displays the function name;-R creates read-only variables (declare and typeset, cannot be reversed with +);-X creates a transfer-out variable;-I creates an integer variable; use + instead--you can reverse the meaning of the option

Export the variable used to create the child shell. --Indicates the end of the option, all subsequent arguments are arguments;-F indicates that the name in the "name-value" pair is the function name;-N Converts the global variable to a local variable, in other words, the command's variable is no longer passed to the shell;-p Displays the list of global variables

ReadOnly is used to set or display read-only variables. --Indicates the end of the selection;-F creating read-only variables

Set or reset various shells

Shift is used to move position variables, adjust position variables, for example shift 1 assigns the value of $ $ to $ $, and Shift 2 assigns the value of $ $ to $ $, and so on.

Typeset is used to display or set variables. It's a synonym for declare.

The unset is used to cancel the definition of a variable. ----F Delete the read-only variable, but you cannot remove the specified variables and functions from the shell environment, such as path, PS1, PS2, UID, PPID, Euid ... The settings

2.10. Quotation marks

The necessity of quotation marks: variable and substitution operations, one of the easiest mistakes to make when performing variable substitution in a script is the reference error. For example, the difference between echo ert* and echo "ert*"

2.11. Double quotes (still in doubt)

Use double quotation marks to reference any character or string other than the character $, ' (anti-quote), \ \

Can test

echo "ERT, $SHELL * \nchina ' echo itlab '"

The output is

ERT,/bin/bash * Chinaitlab

  

2.12. Single quotation marks

Single quotes are similar to double quotes, but the shell ignores any reference values. In other words, if the special meaning is masked, all the characters in the quotation marks, including the quotation marks, are used as a string

Can test

Echo ' ert, $SHELL * \nchina ' echo Itlab '

The output is

ERT, $SHELL * china ' echo Itlab '

2.13. Anti-Quote

Anti-quotes are used to set the output of system commands to variables. The shell takes the contents of the anti-quote as a system command and executes its results, for example, you can test

echo "* China ' echo itlab '"

The output is

* Chinaitlab

2.14. Back slash

If a character has a special meaning, the backslash prevents the shell from misunderstanding its meaning, which is to block its special meaning. The following characters contain special meanings: $ & * + ^ ' "|?

For example, Echo \*, whose output is *, and echo * is the output of all filenames in the current directory

2.15. Operators

An operator is an instruction that is sent to a computer.

Operands: Numbers, characters (sub-denominations), variables, expressions

Expression: The combination of an operator and an operand

2.16. Operator types

Bitwise operator: ~,<<,>>,&,|,^. Same as bitwise operator in C

$[] representation tells the shell to evaluate an expression in parentheses, such as

#!/bin/bashecho $[2+8]echo $[2<<4]

Execution results are

1032

Logical operator: &&,| |,>,<,==,!=. and other languages are the same, for example

#!/bin/bashecho $[2&&2]echo $[2&&0]echo $[2>0]

  

Execution results are

101

Assignment operators: =, + =,-=, *=,/=,%=, &=, ^=, |=, <<=, >>=. It's the same with other languages.

Let count = $count + $change

Let count + = $change

Example:

#!/bin/bashvar=65echo $varlet Var+=4echo $var

Execution results are

6569

2.17. Expression substitution

$[] and $ (()). Accustomed to using $[], the evaluation of all shells is done with integers

$[] can accept numbers of different cardinality. $[Base#n] n represents any cardinality from 2 to 36, such as Execute command echo $[10#8+1], and the result is 9. It means 8 binary 10 to add 1 operation, so the result is 9

2.18. Operator Precedence

Shell Basics Tutorial--2. Variables and operators

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.