pc70
pc70

Reputation: 701

Conditional if operator in bash

Set the value of myval to $1 if it is not empty. If it is empty, set value of myval to val. Is this syntax correct in bash?

#!/bin/bash

val=5
myval=""
if [ "$val" != "" ]; then
    myval=("$1" == "" ? $val : $1)
fi
echo $myval

Upvotes: 1

Views: 196

Answers (1)

chepner
chepner

Reputation: 530920

The syntax is incorrect, but there is a parameter expansion operator to do what you want.

myval=${1:-$val}

Upvotes: 3

Related Questions