026_关于shell中的特殊变量$0 $n $* $@ $! $?

一、

$n:获取当前执行的shell脚本的第N个参数,n=1..9,当n为0时表示脚本的文件名,如果n大于9,用大括号括起来like${10}.

$*:获取当前shell的所有参数,将所有的命令行参数视为单个字符串。
$@:这个程序的所有参数"$1" "$2" "$3" "...",这是将参数传递给其他程序的最佳方式,因此TA会保留所有内嵌在每个参数里的任何空白。
$#:获取当前shell命令行中参数的总个数。

$_:代表上一个命令的最后一个参数

eg:

cat test.sh
#!/bin/bash
echo $_

sh test.sh 1 2 3 4 5
/bin/sh

$!:代表最后执行的后台命令的PID

eg:

➜  agent git:(master) ✗ nohup ./falcon-agent  -c  cfg.json &> var/app.log &
[1] 76901
➜  agent git:(master) ✗ ps -p $!
  PID TTY           TIME CMD
76901 ttys000    0:00.01 ./falcon-agent -c cfg.json
➜  agent git:(master) ✗ echo $!
76901
➜  agent git:(master) ✗ ps -p $!
  PID TTY           TIME CMD
76901 ttys000    0:00.02 ./falcon-agent -c cfg.json
➜  agent git:(master) ✗ echo $!
76901

➜ agent git:(master) ✗ nohup sh ./test.sh &
[2] 79469
appending output to nohup.out
➜ agent git:(master) ✗ echo $!
79469

二、

 

参考:http://www.111cn.net/sys/linux/79750.htm

 

posted @ 2017-11-18 09:46  arun_yh  阅读(297)  评论(0编辑  收藏  举报