| by Arround The Web | No comments

How to Write the Bash If-Else Statement in One Line

The open and improved Bourne shell variant that is offered with the Linux and GNU computer systems is called Bash. Identical to the original, Bash also supports the command-line editing as an addition. The if-else statements are essentially statements that are used to make decisions, and they are quite beneficial. If the given condition is true, the provided if statement is printed. If it is false, the else statement is printed. We can utilize an if-else statement in one line in Bash. Before writing the script, we must add the Bash shell on Linux to run the Bash scripts.

Using the Bash If-Else Statement in Linux

We can use the case statements and if statements to make choices in our Bash programs. They provide us the possibility of running a code piece or not based on circumstances that we can specify. For conditional splitting of a program’s or script’s execution between two pathways, an if-else statement is employed. For instance, if we write a program and include an “if else” condition, the program executes the “if” statement if the provided condition is true. The else statement is executed if the provided condition is false.

We now utilize the Bash “if else” condition. Our Bash file, which ends with the “.sh,” extension, is located on the desktop. So, we must first use the “cd Desktop/” command in the terminal to access the desktop directory. When we run this command, we thereafter obtain the desktop directory.

Linux@linux:~$ cd Desktop/

 
Now, we utilize the “if else” statement in the Bash script to accomplish this. First, we open the Bash file from the desktop. Once it’s open, we use the Bash shell which is “#!/bin/bash” in the first line. Then, wewrite the script for this. First, we put “if” and we use the square brackets. Then, inside these square brackets, we write the condition that reads “if [221] equals [221].”

Here, we essentially utilize the “-eq,” flag which denotes or performs the equals sign’s operation. We add the terminating “;” at the end of the line. Then, in the following line, weuse the “then” keyword. After that, in the following line, we use the “echo” term which is essentially used in Bash to print the statements. Inside of this echo, we use an inverted comma and pass the “The condition is True” statement. Then, in the next line, we use the “else” statement, passing the “The condition is false” statement using the “echo” keyword. The word “fi” is used to end the condition. To end the “if” condition, use this argument.

#!/bin/bash
If [ 221 -eq 221 ];
then
echo “The condition is true
else
echo “The condition is false
fi

 
Now, we open this Bash file on the terminal to inspect the script’s output. To do this, type the command in which first we type the dot slash “./” followed by the name of the script’s Bash file, which is “data.sh”. This command displays the outcome on the console when it is run.

Linux:~/Desktop$ ./data.sh

 
Now, as you can see, when we run this command, the “The condition is true” statement which is in the “if” block is displayed. As a result, the “if” block is performed because this signals that the condition is true. However, if the situation is false, the else expression is shown.

Using the -GT Flag with the If-Else Statement in Bash

Now, in this section, weuse another option which is “-gt” that is essentially used for the greater-than symbol “>”. Let’s begin the script using the Bash shell which is “#!/bin/bash” in the first line. Then, in the next line, we use the “if” condition which states that if “7” is greater than “24”, as we previously explained, where “-gt” is used for the greater than sign, we must write the condition inside square brackets.

Eventually, we use the terminal sign “;” at the end of the line after closing the brackets. Then, we print the “The condition is true” statement which is printed in the output if the condition is met. If the condition is not met, however, we use the else statement. In which case, we print the “The condition is false” statement. Finally, we use the “fi” keyword to end the if-else condition.

#!/bin/bash
If [ 7 -gt 24 ];
then
echo “The condition is true
else
echo “The condition is false
fi

 
Now that we used the “./data.sh” command, we open the output of this Bash in the terminal

Linux:~/Desktop .$ ./data.sh

 
Therefore, after running this command, you can see in the following image that it displays the else statement which reads that the “The condition is false.” This is because the condition is false. After all, “7” is not greater than “24”, so the else block is run.

Using the If-Else Statement with Multiple Conditions

In this section, weuse the if-else statement with several conditions. Let’s begin the script by first using the Bash shell as we did in the previous examples. The “if” condition is then used in the line after that. To do this, use the double square brackets first. Inside of which, we type the “16 -eq 14” condition which means that “16” is equal to “14”, followed by the “and” operator “&&”, and use the “hey == hey” condition. Then, we use the “or” “||” operator and apply the condition if “2 -gt 7”. This means that if “2” is larger than “7”, we print the “The condition is true” statement. In the following line, if the condition is false, we use the else condition to display the “The condition is false” statement.

#!/bin/bash
If [[ 16 –eq 14 && “hey” == “hey” || 2 -gt 7  ]];
then
echo “The condition is true
else
echo “The condition is false
fi

 
To view the output of the Bash script, we now use the “./data.sh” command.

Linux:~/Desktop .$ ./data.sh

 
Since”16″ is not equal to “14” and “2” is not more than “7”, the else statement which reads “The condition is false” is displayed in the output after the command is run, as you can see in the following figure:

Utilizing the Bash If Statement in One Line

In this section, we use the “if” statement in a single line. To do this, we first use the Bash shell which is “#!/bin/bash” just like in the previous example. The “if” expression is then employed in the line after that to determine if a condition exists. First, we type “if”. Then, we use the square brackets inside of this bracket to apply the condition which states that “X==X”. If the condition is true, we then use echo to print “1”. Then, we apply “;”. After which, we use “echo” once more to print “2”. Lastly, we print “3”.

However, because we used the sign “;” after printing each statement, all of this text won’t print in the same line. Instead, it prints in the new lines. Now, in the next line, we apply the same procedure once more. But this time, we update the condition to state that if “X==Y” three lines are printed. The first line prints “4”. The second line prints “5”. And the third line prints “6.” Then, we use “fi” to close the condition.

#!/bin/bash
If [ “X” == “X” ]; then echo1” ; echo2” ; echo3” ; fi
If [ “X” == “Y” ]; then echo1” ; echo2” ; echo3” ; fi

 
The statements of the first “if” condition are presented in the output on the following lines when we run the script, as you can see in the following image because that condition is true. It displays “1” in the first line, “2” in the second, and “3” in the final line.

Conclusion

The if-else statement in Linux Bash is discussed in this article. We used a variety of Bash if-else examples in this article. The if-else situations are used with a variety of flags. If the condition is met, the “if” statement is shown. Otherwise, the “else” statement is displayed. The if-else condition is used with many operators at once in the second segment. How to use the “if” statement on a single line is also covered in the third line.

Share Button

Source: linuxhint.com

Leave a Reply