shell编程——内部变量

常用的内部变量有:echo, eval, exec, export, readonly, read, shift, wait, exit 和 点(.)

echo:将变量名指定的变量显示到标准输出
[root@localhost ~]# echo test
test
shift:使所有的位置参数依次向左移动一个位置,并使位置参数$#(个数)减1,直到减为0
[root@localhost ~]# cat a.sh 
echo "$1,$2,$3,$4"
echo "参数个数:$#"
shift
echo "$1,$2,$3,$4"
echo "参数个数:$#"
[root@localhost ~]# sh a.sh 1 2 3 4
1,2,3,4
参数个数:4
2,3,4,
参数个数:3
export:把变量带入子shell,让子进程继承父进程中的环境变量。
语法:export 变量名=value
-bash-4.1$ echo $LANG
zh_CN.UTF-8
-bash-4.1$ export LANG=zh_CN.GB2312
-bash-4.1$ echo $LANG
zh_CN.GB2312

注意:子shell不能使用export把它的变量向上带入父shell
read:从标准输入读取字符串,并传给指定变量。
可以在函数中用(local 变量名)的方式申明局部变量,测试脚本内容如下:
[root@localhost collect]# cat test.sh 
#!/bin/bash
# this script is created by dengtr.
# e_mail:
# qqinfo:
# function:
# version:
################################################
test() {
local name
echo "please input your name:"
read name
echo "this is $name"
}

test;

执行脚本:
[root@localhost collect]# sh test.sh 
please input your name:

#此时脚本等待用户输入,我在这输入tom,结果如下:
[root@localhost collect]# sh test.sh 
please input your name:
tom
this is tom
原文地址:https://www.cnblogs.com/dengtr/p/5027369.html