BASH from beginner to advanced: Solutions to Hackerrank Challenges - Episode 5: Conditionals

Опубликовано: 03 Июнь 2026
на канале: Adam Djellouli
232
4

Welcome to this in-depth series of videos where we will be providing solutions and explanations for various BASH challenges from Hackerrank. In this video, we will be tackling the "Getting started with conditionals" challenge, which can be found at this link: https://www.hackerrank.com/challenges...

Our goal in this series is to document my thought process and solutions, and to focus on concepts and ideas rather than details. I will also be highlighting the differences between BASH and popular programming languages like Python and JavaScript.

BASH is a powerful command-line interface for interacting with an operating system. It's a shell, and the most popular one. It can be used directly in the terminal or in scripts, which are text files containing a series of instructions. BASH is particularly useful for tasks such as text processing, file operations, connecting multiple programs, and system maintenance. It is also portable, working by default on many platforms.

In Bash, an "if statement" is used to perform conditional operations. The basic syntax of an if statement is:

if [condition]; then
commands
fi

The condition is a test that returns either true or false. The commands following the then keyword are executed if the condition is true. The fi keyword marks the end of the if statement.

For example, the following if statement checks if the variable x is equal to 5 and prints a message if it is:

x=5
if [ $x -eq 5 ]; then
echo "x is equal to 5"
fi

You can also include an "else" clause in your if statement, which will execute a different set of commands if the condition is false:

x=5
if [ $x -eq 5 ]; then
echo "x is equal to 5"
else
echo "x is not equal to 5"
fi

If you want to see all the solutions for these challenges, you can find them on my Github repository: https://github.com/djeada/Bash-Hacker...

Also, you can find me on my website: https://adamdjellouli.com/ where I write about different programming topics, and you can see my other projects.

Don't forget to like and subscribe for more BASH tutorials and solutions to Hackerrank challenges.