# Conditionals

## Conditional Statements in Bash

Similar to many programming languages `bash` allows you to write conditional statements to control the flow of a script:

``````if [[ condition ]]
then
clause statement
fi
``````
``````if condition:
clause statement
``````
``````if (condition) {
clause statement
}
``````
``````if (condition) {
clause statement
}
``````

`Bash` requires a closing “`fi`” for `if` statements.

## Operators

Bash has built in binary operators for the following:

• equal to: `-eq`
• not equal to: `-ne`
• less than: `-lt`
• less than or equal to: `le`
• greater than: `-gt`
• greater than or equal to: `-ge`

Some of the above operators will be used in the following walkthroughs for the `Bash: Scripting` chapter.

## Examples

### if Statement

Create a new file called `if-condition.sh`.

``````#!/bin/bash

number1=10

if [[ \$number1 -eq 10 ]]
then
echo "\$number1 is equal to 10!"
fi
``````

Save the above code to the `if-condition.sh` file and exit the editor.

Run the command `bash if-condition.sh`. ### if else statement

Create a new file called `if-else-condition.sh`.

``````#!/bin/bash

number1=1
number2=2

if [[ \$number1 -gt \$number2 ]]
then
echo "The first number is higher"
else
echo "The second number is higher"
fi
``````

Save the above code to the `if-else-condition.sh` file and exit the editor.

Run the command `bash if-else-condition.sh`. ### if elif else statement

Create a new file called `if-elif-else.sh`.

``````#!/bin/bash

new_number=50

if [[ \$new_number -lt 50 ]]
then
echo "The number is less than 50"
elif [[ \$new_number -eq 50 ]]
then
echo "The number is equal to 50"
else
echo "The number is greater than 50"
fi
``````

Save the above code to the `if-elif-else.sh` file and exit the editor. Run the command `bash if-elif-else.sh`.

Try changing the value of `new_number`!

## Recap

• `if` statement syntax
• `if` statement example
• `if else` example
• `if elif else` example
• `fi`: closing if statement
{"ft_lang_label":"__label__en","ft_lang_prob":0.78642356,"math_prob":0.82160443,"size":3114,"snap":"2023-14-2023-23","text_gpt3_token_len":786,"char_repetition_ratio":0.14533763,"word_repetition_ratio":0.13243762,"special_character_ratio":0.24598587,"punctuation_ratio":0.093856655,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98255116,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,2,null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-06-08T08:44:42Z\",\"WARC-Record-ID\":\"<urn:uuid:5a48f301-6e7a-4ba7-b04d-e9195f12bdaa>\",\"Content-Length\":\"66490\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2d57e2f2-f716-452f-b9b7-7ce38c33a034>\",\"WARC-Concurrent-To\":\"<urn:uuid:13bffe43-8488-4f53-9579-b6531a3449ef>\",\"WARC-IP-Address\":\"137.184.31.248\",\"WARC-Target-URI\":\"https://launchcodetechnicaltraining.org/linux/bash-scripting/walkthrough/conditionals/\",\"WARC-Payload-Digest\":\"sha1:ZBKR4R7PGRPXYMSNZGQHFDQMSJDPMU4A\",\"WARC-Block-Digest\":\"sha1:3HTHX6RPIV6NM5EGC3LRWKD3ZHB665GK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224654606.93_warc_CC-MAIN-20230608071820-20230608101820-00651.warc.gz\"}"}