您的位置:首页 > 运维架构 > Shell

Linux学习之shell编程二

2014-03-16 20:38 92 查看
shell script 的默认变量

$0,$1......

在shell脚本里面,执行的脚本文件名就是$0变量,$1就是脚本的第一个参数。还有一些特殊的变量,比如:

$#:代表后接的参数个数

$@:代表$1,$2,$3,$4之意

$*:代表“$1c$2c$3c$4”,其中c为分隔符,默认为空格

shift:可以偏移变量号码,比如shift ,或者shift 2可以将变量代码向左移动1次或2次

#!/bin/bash
#filename:shell05.sh
echo "The number of variables is ==> $#"
echo "The total variable is ==> '$@'"
echo "The first variable is ==> $1"
shift #偏移一次
echo "The number of variables is ==> $#"
echo "The total variable is ==> '$@'"
echo "The first variable is ==> $1"
运行./shell05.sh one two three four

得到结果:

The number of variables is ==> 4
The total variable is ==> 'one two three four'
The first variable is ==> one
The number of variables is ==> 3
The total variable is ==> 'two three four'
The first variable is ==> two
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: