Linux shell $相关的变量

代码示例1

user="yaya" 
echo ${user} #在已定义过的变量前加$符号便可使用该变量,{}是可选的 yaya #输出结果

 代码示例2

cd /tmp
current_working_dir=$(pwd) #()内为linux指令
echo current_working

/tmp #输出结果

代码示例3

编写脚本test.sh

#!/bin/bash

while getopts ":i:o:b" i
do
        case ${i} in
                i) input_dir="$OPTARG";;
                o) out_dir="$OPTARG";;
                b) relax=true;;
        esac
done

echo $#  #执行脚本时输入的参数个数
echo $0  #执行脚本的路径及名称
echo $1  #执行脚本时的第一个参数(大于9时用{})
echo $*  #将执行脚本的输入选项和参数合并为一个字符串
echo $@  #执行脚本时输入选项和参数的列表
echo $?  #执行脚本的返回值,正常运行结束返回值为0
echo $$  #脚本运行的进程ID

运行test.sh结果

~/tmp$ bash test.sh -i ./input/ -o ./output/ -b -a
6
test.sh
-i
-i ./input/ -o ./output/ -b -a
-i ./input/ -o ./output/ -b -a
0
460879

 

posted @ 2023-01-09 15:30  yayagogogo  阅读(36)  评论(0编辑  收藏  举报