EN
Bash - if...else statement
0
points
In this article, we would like to show you if...else statement in Bash.
Quick solution:
if test_expression
then
command_1
else
command_2
fi
Where:
test_expression- is the condition that we want to check,then- executescommand_1if the condition is true,else- executescommand_2if the condition is false,fi- closes theif...elsestatement.
Practical example
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.
#!/bin/bash
if [ -d "path/to/directory" ]
then
echo "The directory exists."
else
echo "The directory does not exist."
fi