有什么区别&QUOT的差异; VAR = $ {VAR:-word}"和" VAR = $ {VAR:=词}"? [英] What is the difference between "var=${var:-word}" and "var=${var:=word}"?

查看:318
本文介绍了有什么区别&QUOT的差异; VAR = $ {VAR:-word}"和" VAR = $ {VAR:=词}"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了这个bash的手册页,但我不明白的区别。我测试了他们两个出来,他们似乎产生同样的结果。

I read the bash man page on this, but I do not understand the difference. I tested both of them out and they seem to produce the exact same results.

我想设置一个变量的默认值,如果值不是通过命令行参数进行设置。

I want to set a default value of a variable if the value was not set via a command-line parameter.

#!/bin/bash

var="$1"
var=${var:-word}
echo "$var"

在code以上呼应如果 $ 1 为空,又呼应的价值 $ 1 如果不为空。那么,这:

The code above echoes word if $1 is null and echoes value of $1 if not null. So does this:

#!/bin/bash

var="$1"
var=${var:=word}
echo "$var"

据Bash的手册页

According to Bash man page,

$ {参数:-word} 使用缺省值。如果参数没有设置或者为空,word的扩展将被替换。否则,参数的值是取代

${parameter:-word} Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

$ {参数:=字} 设定的默认值。如果参数没有设置或者为空,word的扩展被分配到参数。参数的值,然后被取代。位置参数和特别参数可能不以这种方式被分配到。

${parameter:=word} Assign Default Values. If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way.

是不是因为他们是相同和 $ {参数:=字} 少了点更多

Is it that they are the same and the ${parameter:=word} just does more?

推荐答案

您无法看到你的例子的区别,你正在使用 VAR 两次,但你可以看到它与两个不同的变量:

You cannot see the difference with your examples as you're using var two times, but you can see it with two different variables:

foo=${bar:-something}

echo $foo # something
echo $bar # no assignement to bar, bar is still empty

foo=${bar:=something}

echo $foo # something
echo $bar # something too, as there's an assignement to bar

这篇关于有什么区别&QUOT的差异; VAR = $ {VAR:-word}"和" VAR = $ {VAR:=词}"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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