shift命令把所有参数变量左移一个位置,使$2变成$1,$3变成$2,依此类推。原来$1的值将被丢弃,而$0仍将保持不变。如果调用shift命令时指定了一个数值参数,则表示所有的参数将左移指定的次数。
在扫描处理脚本程序的参数时,经常要用到的shift命令,如果你的脚本需要10个或10个以上的参数,你就需要用shift命令来访问第10个及其后面的参数。
例子如下:
#!/bin/bash
my_fun()
{
echo '$0 inside function is '$0
echo '$1 inside function is '$1
echo '$2 inside function is '$2
}
echo '$0 outside function is '$0
echo '$1 outside function is '$1
echo '$2 outside function is '$2
echo '$3 outside function is '$3
echo '$4 outside function is '$4
echo '$5 outside function is '$5
echo '$6 outside function is '$6
echo '$7 outside function is '$7
echo '$8 outside function is '$8
echo '$9 outside function is '$9
shift 9
printf "\n"
echo '$0 outside functionaaaaaaaaaaaaaaaaaa is '$0
echo '$1 outside bbbbbbbbbbbb function is '$1
echo '$2 outside bbbbbbbbbbbb function is '$2
echo '$3 outside bbbbbbbbbbbb function is '${3}aaaaaaaaaaa
echo '$4 outside bbbbbbbbbbbb function is '$4
printf "\n"
echo '$10 outside function is '${10}
echo '$11 outside function is '$11
echo '$12outside function is '$12
printf "\n"
my_fun fp1 "fp2 fp3"
运行:
# ./shift1 one two three four five six seven eight nine ten eleven twelve
输出结果:
$0 outside function is ./shift1
$1 outside function is one
$2 outside function is two
$3 outside function is three
$4 outside function is four
$5 outside function is five
$6 outside function is six
$7 outside function is seven
$8 outside function is eight
$9 outside function is nine
$0 outside functionaaaaaaaaaaaaaaaaaa is ./shift1
$1 outside bbbbbbbbbbbb function is ten
$2 outside bbbbbbbbbbbb function is eleven
$3 outside bbbbbbbbbbbb function is twelveaaaaaaaaaaa
$4 outside bbbbbbbbbbbb function is
$10 outside function is
$11 outside function is ten1
$12outside function is ten2
$0 inside function is ./shift1
$1 inside function is fp1
$2 inside function is fp2 fp3
~#
注意要分清楚:
$10 就是 ${1}0 不是 ${10}
阅读(908) | 评论(0) | 转发(0) |