EN
Bash - if...else statement
0 points
In this article, we would like to show you if...else statement in Bash.
Quick solution:
xxxxxxxxxx
1
if test_expression
2
then
3
command_1
4
else
5
command_2
6
fi
Where:
test_expression
- is the condition that we want to check,then
- executescommand_1
if the condition is true,else
- executescommand_2
if the condition is false,fi
- closes theif...else
statement.
In this example, we use echo
command that displays a line of text that is passed in as an argument. Using if...else
statement we check if directory under given path exists and we display the corresponding message.
xxxxxxxxxx
1
2
3
if [ -d "path/to/directory" ]
4
then
5
echo "The directory exists."
6
else
7
echo "The directory does not exist."
8
fi