Bryce McKenney
Bryce McKenney

Reputation: 13

Bash Escaping @ Symbol In Variable

I'm having an issue with an if statement in a Bash script. Here's the code:

if [[ "$AppleID" -ne "" ]]; then
    echo "<result>${AppleID}</result>"
else
    echo "No user logged in."
fi

Assuming $AppleID is a string with a value of "[email protected]", the error message is as follows:

[[: [email protected]: syntax error: invalid arithmetic operator (error token is "@email.com")

I've tried using sed to escape the characters, like this:

if [[ `echo $(printf '%q' $AppleID) | sed -e 's/[\/&]/\\&/g'` -ne "" ]]; then

But I get the same error. How do I escape the @ symbol?

Upvotes: 1

Views: 82

Answers (1)

chepner
chepner

Reputation: 532333

-ne is for comparing integers. You want !=, or better yet, just use -n (which tests if its argument is a non-empty string):

if [[ -n "$AppleID" ]]; then
    echo "<result>${AppleID}</result>"
else
    echo "No user logged in."
fi

Upvotes: 4

Related Questions