Fork me on GitHub

Bash基础——Shell脚本内部常用环境变量

$@$*区别

不加引号的时候没区别

#! /usr/bin/bash

function print_args_at {
    printf "%s\n" $@ 
    echo $@
}

function print_args_star {
    printf "%s\n" $*
    echo $*
}

print_args_at "one" "two three" "four"
echo "*******************************************"
print_args_star "one" "two three" "four"

输出结果

one
two
three
four
one two three four
*******************************************
one
two
three
four
one two three four
View Code

加引号有区别

#! /usr/bin/bash

function print_args_at {
    printf "%s\n" "$@" 
    echo $@
}

function print_args_star {
    printf "%s\n" "$*"
    echo $*
}

print_args_at "one" "two three" "four"
echo "*******************************************"
print_args_star "one" "two three" "four"


输出结果

one
two three
four
one two three four
*******************************************
one two three four
one two three four
View Code

 

 

 

#:$#变量是命令行参数或位置参数的数量

-:$-变量是传递给shell脚本的执行标志

?:$? 变量是最近一次执行的命令或shell脚本的出口状态

$:$$ 变量是shell脚本里面的进程ID。Shell脚本经常使用 $$ 变量组织临时文件名,确保文件名的唯一性。

_:$_ 变量是一个特殊的变量,在shell开始运行时,变量的初始值为shell或其执行的shell脚本的绝对路径名,之后就是最近执行的命令的最后一个选项或参数等。

!:&! 变量的值是最近运行的一个后台进程的PID

LINENO:调试用,用于显示脚本中当前执行的命令的行号。

OLDPWD:利用cd命令改换到新目录之前所在的工作目录。用法:cd $OLDPWD  (切换到之前的工作目录,和cd - 功能一样)

OPTARG:getopts命令已经处理的前一个选项参数。

OPTIND

PPID:是当前进程的父进程的PID

PWD:当前工作目录。这是一个变量,变量值等同于命令pwd的输出

RANDOM:随机数变量。每次引用这个变量会得到一个0~32767的随机数。

REPLY: read命令,如果没有被指定变量。即通过read读入的数据,如果没有对应的变量进行赋值,则可以把REPLY变量用作read命令的默认变量,接收read命令读入的参数。

SECONDS:脚本已经运行的时间(以秒为单位)

posted @ 2018-10-20 11:06  克拉默与矩阵  阅读(842)  评论(0编辑  收藏  举报