这个 bash 语法是什么意思?
这是否意味着如果设置了 ETL_PORT ,则 ETL_PORT 变量将为 ETL_PORT ,否则如果未设置,则默认为 6090 ?
ETL_PORT="${ETL_PORT:-6090}"
答案1
如果该变量在调用时未设置,则该构造${var:-val}
将使用默认值。它只是许多方便的参数扩展之一。我有一个脚本可以生成这个有用的备忘单:val
var
${V} Base string |reallyextremelylongfilename.ext
--- Default substitutions ---
${nullvar} Provided example case |
${#nullvar-def} Default value if unset or null |def
${#nullvar:-def} Default value if unset |def
--- Default assignments ---
${1} Provided example case |
${$1=def} Default value if unset or null |def
${$1:=def} Default value if unset |def
--- String metadata ---
${#V} String length |31
--- Substring extraction ---
${V:6} Substring from position |extremelylongfilename.ext
${V:6:9} Substring with length from pos. |extremely
--- Substring deletion ---
${V#*a} Delete shortest prefix match |llyextremelylongfilename.ext
${V##*a} Delete longest prefix match |me.ext
${V%e*} Delete shortest suffix match |reallyextremelylongfilename.
${V%%e*} Delete longest suffix match |r
--- Substring replacement ---
${V/long/short} Replace first match |reallyextremelyshortfilename.ext
${V/#r*a/REA} Replace prefix match |REAme.ext
${V/%.e*/.dat} Replace suffix match |reallyextremelylongfilename.dat
${V//e/II} Replace all matches |rIIallyIIxtrIImIIlylongfilIInamII.IIxt
--- Other handy things ---
${V?Message} exit 1 with 'Message' output if V is not set or is null
${V:?Message} exit 1 with 'Message' output if V is not set
${V+Value} If V is set, use 'Value', otherwise null