Skip to content

Modifying varaible resolving #645

Closed Answered by McDutchie
shubhamshirude2512 asked this question in Q&A
Discussion options

You must be logged in to vote

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:

if [[ $x == "a" || ! -v x ]]
then
     echo "in if block"
fi

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.

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@shubhamshirude2512
Comment options

@McDutchie
Comment options

@shubhamshirude2512
Comment options

Answer selected by McDutchie
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants