PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, December 22, 2022

[FIXED] How to do complex conditionals in Bash (mix of 'and' &&, 'or' || ...)

 December 22, 2022     bash, conditional-statements, syntax     No comments   

Issue

How do I accomplish something like the following in Bash?

if ("$a" == "something" || ($n == 2 && "$b" == "something_else")); then
  ...
fi

Solution

You almost got it:

if [[ "$a" == "something" || ($n == 2 && "$b" == "something_else") ]]; then

In fact, the parentheses can be left out because of operator precedence, so it might also be written as

if [[ "$a" == "something" || $n == 2 && "$b" == "something_else" ]]; then


Answered By - Niklas B.
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing