如何bash的参数扩展工作? [英] How does bash parameter expansion work?

查看:134
本文介绍了如何bash的参数扩展工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解与谷歌的帮助bash脚本:)

I'm trying to understand a bash script with help of google :)

然后,我偶然发现了这一点:

Then I stumbled upon this:

DIR=${1:-"/tmp"}

这是什么意思?谷歌没有提供任何相关的结果:(

What does that mean? Google does not give any relevant result :(

推荐答案

: - 其实它说的操作,如果 $ 1 (第一个参数脚本)未设置或为然后用 / tmp目录作为值 $ DIR ,如果它被设置为它指定的值 $ DIR

:- is actually an operator it says that if $1 (first argument to the script) is not set or is null then use /tmp as the value of $DIR and if it's set assign it's value to $DIR.

DIR=${1:-"/tmp"}

是短期的

if [ -z $1 ]; then
        DIR='/tmp'
else
        DIR="$1"
fi

它可以与任何变量使用不仅仅是位置参数:

It can be used with any variables not just positional parameters:

$ echo ${HOME:-/tmp} # since $HOME is set it will be displayed.
/home/codaddict
$ unset HOME   # unset $HOME.
$ echo ${HOME:-/tmp} # since $HOME is not set, /tmp will be displayed.
/tmp
$ 

这篇关于如何bash的参数扩展工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆