2

I have a script that I am making, it is a very simple login script.

I was wondering if you could have two expressions in one bash "IF" statement, like so:

if [ $User == "root" and $Pass == "passwd" ]

If anyone could answer, that would be great :D

2 Answers 2

5

In bash, you should use the conditional expression

if [[ $User == root && $Pass == passed ]];

If you need to or want to use test, then join two test commands with &&:

if [ "$User" = root ] && [ "$Pass" = passed ];

Be sure to quote any parameter expansions that are used as arguments to [, and use = instead of ==.

Sign up to request clarification or add additional context in comments.

Comments

0

Use -a for and. And, = instead of ==. Also you'd better double quote variables, otherwise you'll see a syntax error when the variables are not set.

if [ "$User" = "root" -a "$Pass" = "passwd" ]

1 Comment

-a was declared obsolete and should not be used. Instead, join two commands with &&: [ "$User" = root ] && [ "$Pass" = passed ];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.