Linux Learning shell script-------variables

Source: Internet
Author: User

[This is my own study notes, welcome reprint, but please specify the source:http://blog.csdn.net/jesson20121020]

Take a look at some of the variables in the shell today.

Variable Type:

There are several variables in the shell:

1) Local Variables

2) Environment variables

3) Position variable

4) Standard variables

5) Special variables

  

1) Local variables

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

Usage: variable-name=value

Set: can display all local variables

Note that if you add readonly to a local variable, the local variable cannot be modified, that is, the variable is only readable, and you can directly enter ReadOnly or READONLY-P to print out the local variable, for example:

[email protected]:~$ tip= "Hello world!" [email protected]:~$ Set | grep tiptip= ' Hello world! ' [email protected]:~$ Echo $TIPHello world! [email protected]:~$ echo ${tip}}hello world!} [email protected]:~$ tip= "Hello world!" [email protected]:~$ Echo $TIPhello world! [email protected]:~$ readonly tip[email protected]43sv:~$ tip= "Hello shell!" Bash:tip: read-only variable [email protected]:~$ readonly declare-r bashopts= "Checkwinsize:cmdhist:complete_fullquote: Expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:progcomp:promptvars:sourcepath " Declare-ir bashpiddeclare-r bash_completion_compat_dir= "/etc/bash_completion.d" Declare-ar BASH_REMATCH= ' ([0]=] \${ TIP "[1]=" \${"[2]=" Tip ") ' Declare-ar bash_versinfo= ' ([0]=" 4 "[1]=" 3 "[2]=" one "[3]=" 1 "[4]=" Release "[5]=] x86_64- Pc-linux-gnu ") ' Declare-ir euid=" "Declare-ir ppid=" 2162 "Declare-r shellopts=" Braceexpand:emacs:hashall: Histexpand:history:interactive-comments:monitor "Declare-r tip=" HEllo world! " Declare-ir uid= "[email protected]:~$ readonly-pdeclare-r bashopts=" Checkwinsize:cmdhist:complete_ Fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:progcomp:promptvars: SourcePath "Declare-ir bashpiddeclare-r bash_completion_compat_dir="/ETC/BASH_COMPLETION.D "Declare-ar BASH_REMATCH = ' ([0]= "\${tip" [1]= "\${" [2]= "TIP") ' Declare-ar bash_versinfo= ' ([0]= "4" [1]= "3" [2]= "one" [3]= "1" [4]= "Release" [5]=] X86_64-pc-linux-gnu ") ' Declare-ir euid=" "Declare-ir ppid=" 2162 "Declare-r shellopts=" Braceexpand:emacs:hashall: Histexpand:history:interactive-comments:monitor "Declare-r tip=" Hello world! " Declare-ir uid= "1000"

2) Environment variables

   Environment variables are used for all user processes (often referred to as child processes), and the logon process is called the parent process. User processes executed in the shell are called child processes. Local variables are used only for local users ' processes, while environment variables are available for all child processes, including editors, scripts, and applications.

   The export variable can be set to an environment variable, and you can enter the Export command or the ENV command directly to see all the environment variables , such as :

[Email protected]:~$ export name= "jesson20121020" [Email protected]:~$ env | grep namename=jesson20121020

 

3) Position variable

There are 9 variables in the shell, that is, $,..., $9, and more than 9 are implemented in other ways, refer to the following script:

parm.sh

#!/bin/bash#parmecho "script name: the first parameter of the" $0echo "script: The second parameter of the" $1echo "script: The third parameter of the" $2echo "script: The fourth argument of the" $3echo "script: The fifth parameter of the" $4echo "script Number: The sixth parameter of the "$5echo" script: The seventh parameter of the "$6echo" script: The eighth parameter of the "$7echo" script: The Nineth parameter of the "$8echo" script: "$9
Run the above script to get:

[Email protected]:~/develop/workspace/shell_workspace$./parm.sh A B C D E F G script name: The first parameter of the./parm script: The second parameter of a script: the third parameter of the B script: The fourth parameter of the C script: the fifth parameter of the D script: the sixth parameter of the E script: the seventh parameter of the F script: the eighth parameter of the G script: the Nineth parameter of the script:

Look at a script to find files in the current directory by file name

searchfile.sh

#!/bin/bash#searchfilefind. -name $1-print
If we are looking for the parm.sh file in the current directory, we can do this:
[Email protected]:~/develop/workspace/shell_workspace$./searchfile.sh parm.sh./parm.sh
Can see, find it, and print it out.

4) Standard variables

Bash establishes some standard environment variables by default, which can be defined in/etc/profile, mainly Exinit,home (home directory), IFS (default delimiter), Logname,mail,mailcheck (default), Mailpath, PATH,PSI,PS2 (used to run multiple commands on one line), Shell,term (terminal), TZ (timezone), PWD (current directory), MANPATH (Help document directory), Terminfo,editor, etc.

5) Special variables

$#: Number of arguments passed to script

$*: Displays all parameters passed to the script in a single string, which can have more than 9 parameters, unlike positional variables.

$$: The current process ID number for the script to run.

$!: The process ID number of the last process running in the background.

[Email protected]: with $ #相同, but use quotation marks, and return each parameter in quotation marks.

$-: Displays the current options used by the shell, same as the SET command function.

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

We modify the above parm.sh script as follows:

parm.sh

#!/bin/bash#parmecho "script name: the first parameter of the" $0echo "script: The second parameter of the" $1echo "script: The third parameter of the" $2echo "script: The fourth argument of the" $3echo "script: The fifth parameter of the" $4echo "script  Number: The sixth parameter of the "$5echo" script: The seventh parameter of the "$6echo" script: The eighth parameter of the "$7echo" script: The Nineth parameter of the "$8echo" script: "$9echo" shows the number of parameters: "$ #echo" show script All parameters: "$*echo "Displays the current script's process ID number:" $ $echo "shows the status of the previous command running:" $?
Run it and the results are as follows:

[Email protected]:~/develop/workspace/shell_workspace$./parm.sh A B C D E F G script name: The first parameter of the./parm.sh script: The second parameter of a script: the third of the script B Parameters: The fourth parameter of the C script: the fifth parameter of the D script: the sixth parameter of the E-script: The seventh parameter of the F script: the eighth parameter of the G script: the Nineth parameter of the script: 7 Display the parameters: the total number of values shown in the script: A B C D E F g shows the process ID number of the current Status of the command run: 0
There are 5 kinds of variables in the shell, so let's look at some of the variables related commands.

variables-related commands:variable Substitution

Variable substitution replaces its name with the value of the variable.

Add $ to the variable name to print out the value of the variable using the echo $ variable name.

There are several ways to replace a variable:

Variable substitution method

Description

$variable-name

Reference actual value to < Span lang= "en-US" >variable-name

${variable-name}

Ibid.

${variable-name:+value}

If Variable-name is set, its value is displayed, otherwise, it is empty

${variable-name:?value}

If variable-name, displays user-defined error messages.

${variable-name:-value}

If variable-name, displays its value

${variable-name:=value}

If Variable-name is not set , its value is set and displayed

   The following example shows how these variables are replaced using the method and effect

[Email protected]:~$ var= "Hello world!" [Email protected]:~$ echo $varhello world! [Email protected]:~$ echo ${var}hello world! [Email protected]:~$ echo ${var:+ "Hello shell!"} Hello shell! [Email protected]:~$ echo ${var1:+ "Hello shell!"} [Email protected]:~$ echo ${var:?] Define "}hello world! [Email protected]:~$ echo ${var:-"Hello shell!"} Hello world! [Email protected]:~$ echo ${var1:-"Hello shell!"} Hello shell! [Email protected]:~$ echo $var 1[email protected]:~$ echo ${var:= "Hello shell!"} Hello world! [Email protected]:~$ echo ${var1:= "Hello shell!"} Hello shell! [Email protected]:~$ echo $var 1hello shell!
   since there is variable substitution, then there must be a corresponding variable cleared. variable Cleanup

You can clear variables that have been set.

Usage: unset variable-name

[Email protected]:~$ echo $var 1hello shell! [Email protected]:~$ unset var1[email protected]:~$ echo $var 1
   commands that affect variables

Declare

Set or display a variable

-F: Show only function names

-r: Create read-only variables (declare and typeset)

-X: Create a go-out variable

-I: Creating integer variables

Use + override-to reverse the meaning of the option

Export

Used to create a variable passed to the child shell

--: Indicates the end of the option, and all subsequent parameters are arguments.

-F: Indicates that the name is a function name in a "name-value" pair

-N: Converts global variables to local variables. In other words, the variables of the command are no longer passed to the shell.

-P: Show global variable list

ReadOnly

Used to display or set read-only variables

--: Indicates end of option

-F: Create read-only variables

Set

Set or reset various shells

Shift [N]

For moving position variables, adjust position variables.

Typeset

Used to display or set variables

It's a synonym for declare.

unset

Used to cancel the definition of a variable

--Indicates end of option

-F: Removes read-only variables, but cannot remove specified variables and functions from the shell environment, such as Path,psi,ps2,ppid,uid,eudi.

In fact, most of these variables have been used, and we'll focus on the usage and significance of shift, as follows:

parm.sh

#!/bin/bash#parmecho "script name: the first parameter of the" $0echo "script: The second parameter of the" $1echo "script: The third parameter of the" $2echo "script: The fourth argument of the" $3echo "script: The fifth parameter of the" $4echo "script  Number: The sixth parameter of the "$5echo" script: The seventh parameter of the "$6echo" script: The eighth parameter of the "$7echo" script: The Nineth parameter of the "$8echo" script: "$9echo" shows the number of parameters: "$ #echo" show script All parameters: "$*echo "Displays the current script's process ID number:" $ $echo "shows the status of the previous command: the first parameter of the" $?shiftecho "script: The second parameter of the" $1echo "script:" $
Operation Result:

[Email protected]:~/develop/workspace/shell_workspace$./parm.sh A B C D E F G script name: The first parameter of the./parm.sh script: The second parameter of a script: the third of the script B Parameters: The fourth parameter of the C script: the fifth parameter of the D script: the sixth parameter of the E-script: The seventh parameter of the F script: the eighth parameter of the G script: the Nineth parameter of the script: 7 Display the parameters: the total number of values shown in the script: A B C D E F g shows the process ID number of the current Status of the command run: 0 The first parameter of the script: the second parameter of the B script: C
Next, let's change it and add 2 after shift, as follows:

parm.sh

#!/bin/bash#parmecho "script name: the first parameter of the" $0echo "script: The second parameter of the" $1echo "script: The third parameter of the" $2echo "script: The fourth argument of the" $3echo "script: The fifth parameter of the" $4echo "script  Number: The sixth parameter of the "$5echo" script: The seventh parameter of the "$6echo" script: The eighth parameter of the "$7echo" script: The Nineth parameter of the "$8echo" script: "$9echo" shows the number of parameters: "$ #echo" show script All parameters: "$*echo "Displays the current script's process ID number:" $ $echo "shows the status of the previous command: the first parameter of the" $?shift 2echo "script: The second parameter of the" $1echo "script:" $
The results of the operation are as follows:
[Email protected]:~/develop/workspace/shell_workspace$./parm.sh A B C D E F G script name: The first parameter of the./parm.sh script: The second parameter of a script: the third of the script B Parameters: The fourth parameter of the C script: the fifth parameter of the D script: the sixth parameter of the E-script: The seventh parameter of the F script: the eighth parameter of the G script: the Nineth parameter of the script: 7 Display the parameters: the total number of values shown in the script: A B C D E F g shows the process ID number of the current Status of the command run: 0 The first parameter of the script: the second parameter of the C script: D

Linux Learning shell script-------variables

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.