How to build a conditional assignment in bash?

后端 未结 8 2104
慢半拍i
慢半拍i 2021-01-31 01:04

I\'m looking a way to build conditional assignments in bash:

In Java it looks like this:

int variable= (condition) ? 1 : 0;
8条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-31 01:49

    If you want to assign a value unless variable is empty use this:

    [ -z "$variable" ] && variable="defaultValue"
    

    You can put as well, each other condition on the []

提交回复
热议问题