live_tv
Livestream Starting Soon
00
Hours
:
00
Minutes
:
00
Seconds
Up next in 10 CANCEL
PLAY NOW
Bash if else Statement | Bash Scripting - Else If Statement | Linux Shell If-Else Syntax Example Nov 2, 2023
The if else statement in Bash is used to conditionally execute code based on the evaluation of an expression. The syntax of the if else statement is as follows:
Bash
if [ expression ]; then
# code to execute if the expression is true
else
# code to execute if the expression is false
fi
Use code with caution. Learn more
The expression can be any valid Bash expression, such as a comparison expression, a logical expression, or a test of a file or directory. If the expression evaluates to true, then the code in the then clause is executed. Otherwise, the code in the else clause is executed.
Here is an example of a simple if else statement:
Bash
#!/bin/bash
# Check if the file exists
if [ -f myfile.txt ]; then
# File exists, so print its contents
cat myfile.txt
Show More Show Less Recommended Videos
play_arrow
7:16
play_arrow
12:03
play_arrow
1:35:03
play_arrow
16:20
play_arrow
5:28
1
2:46
Bash Script – File Permissions | File Permissions in Bash Scripting
Aug 14, 2023
2
3:00
Bash Scripting – While Loop | Loops in Scripting
Aug 14, 2023
3
2:02
Bash Scripting – String | Strings in Bash Scripting
Aug 14, 2023
4
2:27
Bash Scripting – Case Statement | Case Statements in Scripting
Aug 14, 2023
5
2:46
Bash Script – Arithmetic Operators | Advanced Arithmetic Operations
Aug 14, 2023
6
2:16
Bash Scripting – Working of Alias | Aliases in Bash Scripting
Aug 15, 2023
7
2:46
Bash Scripting – Substring | Capabilities of Substrings in Bash Scripting
Aug 15, 2023
8
2:12
Bash Script – Working of Bash Variables | Functionality of Variables in Bash Scripting
Aug 15, 2023
9
2:40
Batch Script – Iterating Over an Array | Array Iteration in Batch Scripting
Aug 15, 2023
10
2:16
Bash Script – Read User Input | User Input in Bash Scripting
Aug 15, 2023
11
2:32
Sed Command in LinuxUnix with examples | Advanced 'sed' Commands
Aug 15, 2023
12
1:40
Bash if else if | Bash scripting | Bash scripting capabilities
Aug 17, 2023
13
2:13
Bash if Multiple Conditions | Building on Basic 'if' Statements | Advanced Bash scripting
Aug 17, 2023
14
2:06
bash if -d | the '-d' flag within 'if' statements | '-d' flag in Bash scripting
Aug 17, 2023
15
2:48
if-else in shell script | 'if-else' statements in shell scripting | using 'if-else' constructs
Aug 17, 2023
16
2:44
bash if not | 'if-not' logic in shell scripting | 'if-not' constructs
Aug 17, 2023
17
2:30
bash if -f file | 'if' statement with the '-f' flag | checks in shell scripting
Aug 17, 2023
18
2:06
if-else in shell script example | if-else statements in shell scripting | if-else statements contribute
Aug 17, 2023
19
2:00
shell script if -z option | if -z' option in shell scripting | utilizing the '-z'
Aug 17, 2023
20
2:51
How to Use Shell Scripting in Linux | shell scripting in Linux
Aug 19, 2023
21
1:57
How to Write a Bash Script: A Simple Bash Scripting Tutorial | Bash scripting tutorial for beginners
Nov 9, 2023
22
2:39
How to Rename Files and Directories in Linux | Rename a File in Linux – Bash Terminal Command
Nov 7, 2023
play_arrow
3:10
Bash if else Statement | Bash Scripting - Else If Statement | Linux Shell If-Else Syntax Example
Nov 2, 2023
24
2:06
if else in shell script example | Conditions in bash scripting (if statements) | Shell Scripts
Oct 29, 2023
25
1:48
How to Write a Bash Script: A Simple Bash Scripting Tutorial | Shell Scripting Tutorial
Oct 29, 2023