1

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
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
pc70
  • 681
  • 1
  • 14
  • 28

1 Answers1

3

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

myval=${1:-$val}
chepner
  • 497,756
  • 71
  • 530
  • 681