To interpret variables through shell scripts

Source: Internet
Author: User

    1. Variable assignment, substitution, etc.

#!/bin/bash
#1. Assigning values to a single variable
Variable1=33 #将变量variable1赋值为33
Echo ${variable1} # #第一种显示方法为 ${variable1}
echo $variable 1 # #第二种显示方法为 $variable 1

variable2= "Hello World" #将变量variable2赋值为 "Hello World", need double quotes to avoid character separation
Echo $variable 2
#****************************************************************************

#2. Variable Set Variable assignment value
Variable4= "We are saying $variable 2" #变量赋值中使用另一个变量 $variable 2
Echo $variable 4

#*****************************************************************************

#3. Clears the variable, but does not initialize the variable.
unset variable4 #清除variable4变量
Echo $variable # #空白行表示variable4变量未被初始化

#*****************************************************************************

#4. Symbols: + and Symbols:-Same points and differences
#相同点: You cannot reassign a variable that has already been assigned a value
Colour=black #对变量colour赋值
echo "The Background is ${colour:=blue}" usage of the ##:= symbol: represents an unassigned variable, assigns value to it
echo "The Background is ${colour:-blue}" uses the ##:-notation: represents an unassigned variable, assigns value to it, but value value is not stored in the corresponding address space of variable
#区别:
Unset colour #将变量colour的赋值清除
echo "The Background is ${colour:-blue}" # #用:-Symbol to colour assignment
Echo $colour # #colour没有真正赋值
echo "The Background is ${colour:=blue}" # #用: = symbol assigned to colour
Echo $colour # #colour赋值了

#*******************************************************************************

#5. Symbol + variable The assigned value, reset its value
Echo ${colour+red} # #对已经赋值的colour, re-assigned to red, temporary assignment
Echo $colour # #显示原来的赋值

#******************************************************************************

#6.?   Or:? Displays system error messages for unassigned variable
# #变量color是否赋值, if you have already assigned a value, the assignment is displayed, and no assignment shows-bash:b:parameter null or not set
echo ${colour?} # #变量colour已经赋值了, the assignment is shown below
unset Colour # #清除colour赋值赋值
echo ${colour?} # #colour没有赋值,-bash:b:parameter null or not set is displayed below


#/bin/bash
Colour=blue # #将变量colour赋值
echo Colour
Read Only colour # #将变量colour赋值为只读
Colour=black # #将变量colour重新赋值, found unable to assign value
ReadOnly # #查看系统中所有的只读变量

=========================================================

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


parameter of the variable is used

#!/bin/bash
Echo "The script name is: $"       # $ to indicate the script itself
Echo "Parameter #1: $"             # Represents a 1th argument, followed by
Echo Parameter #2: $ "
echo "Parameter #3: $"
echo "Parameter #4: $4"
echo "Parameter #5: $"
echo "Parameter #6: $6"
Echo "Param Eter #7: $7 "
echo" Parameter #8: $8 "
echo" Parameter #9: $9 "
echo" Parameter #10: ${10} "    # ${10} Use curly braces

echo "-------------------------"
echo "The number of command line parameters is: $#" # $ #表示传递到脚本的参数数量
echo "All of the command line parameters is: [email protected]" # [email protected] or $* all parameters passed to the script
echo "The process ID is: $$" # $$ indicates the process number that the script is running
echo "Did this script has any errors $?" # $? Indicates the exit status of a command, 0 means no error, and 0 indicates an error

#position脚本说明 $0,$1,$2,..., ${10},$*,$#,$$,$?
#这里用cat tr cut head tail sed awk less more paste to represent 10 parameters of $ ${10}


========================================================

++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Relationship of parent and child processes


#father. SH: The script outputs its own process and invokes the child process

#!/bin/bash
#输出父进程号
echo "Father Process ID is $$"

#定义本地变量并输出
Localvar= "Define a local variable."
echo "Localvar= $localvar"

#定义环境变量并输出
envvar= "Define a environment variable."
Export Envvar
echo "Envvar= $ENVVAR"

#调用child. Sh script, which is the creation of child processes
$PWD/child.sh

#child. Sh execution complete, output correlation variable value
echo "Return to Father process: $$"
echo "Localvar= $localvar"
echo "Envvar= $ENVVAR"



#child. SH: The script outputs its own process number and parent process number, redefining local variables and environment variables
#!/bin/bash

#输出自身进程号及父进程号
echo "Chile Process ID is $$"
echo "My Father Process ID is $PPID"

#输出本地变量和环境变量的当前值
echo "Localvar= $localvar"
echo "Envvar= $ENVVAR"


#重新定义本地变量和环境变量
Localvar= "Redefine this ocal variable."
envvar= "Redefine This environment variable."
echo "Localvar= $localvar"
echo "Envvar= $ENVVAR"



This article is from the "All Night" blog, please make sure to keep this source http://endmoon.blog.51cto.com/8921900/1616629

To interpret variables through shell scripts

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.