Modifying varaible resolving #645
-
Hello, for example if [[ "a" -eq $x ]] Now the variable x is not not defined so echo $x prints nothing but still should execute the if block. I know this sounds wierd but I was just wondering if we could alter variable assignment in ksh source code. Thanks in advance |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
ksh does not need modifying here, your script does. For one thing, using So, if you want the if [[ $x == "a" || ! -v x ]]
then
echo "in if block"
fi The Hope this helps. |
Beta Was this translation helpful? Give feedback.
ksh does not need modifying here, your script does. For one thing, using
-eq
is wrong there as that compares numerical values or arithmetic expressions; you want==
for string (pattern) comparison.So, if you want the
if
block to be executed if either $x is "a" or x is not defined (a.k.a. unset), you want:The
-v
operator to[[
checks if the variable is set. The!
negates it so the condition is true if the variable is unset. If you want to check whether it is unset or set to empty, then you need-z
instead. See the manual page for more details.Hope this helps.