1、脚本中使用位置参数
#!/bin/bash
#param
echo "this is script name : $0"
echo "this is the first name : $1"
echo "this is the second name: $2"
echo "this is the three name : $3"
这里传递3个参数,$0是脚本名自身;
注意:$0返回的是当前目录路径,如下
#!/bin/sh
#param2
echo "hello world this is $0 calling"
test@linux_NGIN:~/script> ./param2
hello world this is ./param2 calling ------当前目录路径
#!/bin/sh
#param2
echo "hello world this is `basename $0` calling" ------使用basename获取脚本名称
test@linux_NGIN:~/script> ./param2
hello world this is param2 calling
2、向系统命令传递参数
#!/bin/sh
#findfile
find / -name $1 -print -------查找文件名
#!/bin/sh
#who_is
grep $1 /etc/passwd | awk -F: '{print $1 "\t" $6}' ---------查找当前用户名并输出其home目录
3、特定变量参数
$# 传递到脚本的参数个数
$* 以一个单字符串显示所有向脚本传递的参数。与位置变量不同,此选项参数可超过9个
$$ 脚本运行的当前进程ID号
$! 后台运行的最后一个进程的进程ID号
$@ 与$#相同,但使用时加引号,并在引号中返回每个参数
$- 显示SHELL当前选项,与set命令相同
$? 显示最后命令的退出状态,0为没有错误,其他任何值表明有错误