Shell Variable Introduction

Source: Internet
Author: User

Variable naming rules
变量名必须以字母或下划线开头,名字中间只能由字母,数字和下划线组成,大小写是区分的变量名的长度不得超过255个字符变量名在有效的范围内必须是唯一的在Bash中,变量的默认类型都是字符串类型"用户自定义变量" ? ?变量自定义的"环境变量"  ? ?这种变量中主要保存的是和操作环境相关的数据。变量可以自定义,但是对系统生效的环境变量名和变量作用是固定的。"位置参数变量" ?  ? ?这种变量主要是用来向脚本当中传递参数或数据的,变量名不能自定义,变量作用是固定的"预定义变量" ? ?是Bash中已经定义好的变量,变量名不能自定义,变量作用也是固定的 ? ?"可以将一个命令的执行结果赋值给变量,但是需要使用命令替换符号"[[email protected] shell]# cat start.sh #!/bin/bashecho 'ssgao ai xiaoxiao'mypath=`pwd` //使用命令替换符,或来获取命令执行结果echo $mypath?[[email protected] shell]# cat pwd.sh #!/bin/bashmypath=$(pwd) //使用$()来获取命令执行结果echo $mypath[[email protected] shell]# sh pwd.sh /root/shell?"注意单引号和双引号的区别"注意单引号和双引号的区别,""号会把里面的变量值进行输出,''是把内容原封不动的输出,不会识别里面的变量。[[email protected] shell]# cat start.sh #!/bin/bashecho 'ssgao ai xiaoxiao'mypath=`pwd`touch log.logifconfig >log.logpatha="当前路径:${mypath}"pathb='当前路径: ${mypath}'echo $mypath //输出:/root/shellecho $patha //输出:当前路径:/root/shellecho $pathb //输出:当前路径: ${mypath}
Custom variables
'变量名=变量值' ? ?1) 变量不能使用数字开头 ? ?2) '='左右两侧不能加空格: ? ?name="jie cao" //变量值有空格需要用""括起来 ? "变量调用" ? ?echo $变量名 //调用变量,在变量名前面加'$'符号 ? ?192:~ aouo$ y=6 //声明变量 ? ?192:~ aouo$ x=5 //声明变量 ? ?192:~ aouo$ z=$x+$y ?//调用变量x和变量y ? ?192:~ aouo$ echo $z //结果为5+6"变量的叠加" ? ? ? ?x=123 ? ?x="$x"456 ? ?x=${x}456 ? ?----------------- ? ?192:~ aouo$ x=123 ? ?192:~ aouo$ echo $x // 结果为123 ? ?192:~ aouo$ x="$x"456 ? ?192:~ aouo$ echo $x //结果为123456 ?"查看所有变量" ? ? [[email protected] shell]# ?set ? ? ? STORM_HOME=/root/storm/apache-storm-1.1.1 ? ? TERM=xterm ? ? UID=0 ? ? USER=root"删除变量"  ? ?unset 变量名 ? ?  [[email protected] shell]# name=ssgao //声明变量 ?  [[email protected] shell]# echo $name //输出ssgao ?  [[email protected] shell]# unset name //删除变量 ?  [[email protected] shell]# echo $name -bash: name: unbound variable
Positional parameter variables
$nn为数字,$0代表命令本身,$1~$9代表第一到第九个参数,十以上的参数需要用大括号包含,如${10}$*这个变量代表命令行中所有的参数,$*把所有的参数看成一个整体[email protected]这个变量也代表命名行中所有的参数,不过[email protected]把每个参数区分对待$#这个变量代表命令行中所有参数的个数
192.168.10.101:shell_start aouo$cat add.sh #!/bin/bashno1=$1no2=$2no3=$(($no1+$no2))#变量no3的和是no1加no2echo $no3#打印变量no3的值-------------------------------------------------192.168.10.101:shell_start aouo$./add.sh 1 1011-------------------------------------------------$1 = 1 位置参数1$2 = 10 位置参数2
Pre-defined variables
$?最后一次执行的命令返回状态,如果这个变量的值为0,证明上一次命令正确执行。如果这个变量的值非0证明上一个命令执行不正确了$$当前进程的进行号(PID)$!后台运行的最后一个进程的进程号(PID)

Shell Variable Introduction

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.