Unlock AI power-ups — upgrade and save 20%!
Use code STUBE20OFF during your first month after signup. Upgrade now →

By Learn Linux TV
Published Loading...
N/A views
N/A likes
Bash Scripting: Introduction to If Statements
📌 The video introduces the if statement in Bash scripting, noting that Bash acts as a command interpreter with features similar to programming languages.
⚙️ The basic structure of an `if` statement involves a test case enclosed in brackets (e.g., `[ my_num -eq 200 ]`), followed by `then` for actions if true, and closed with `fi`.
➕ The use of `-eq` is demonstrated for checking numerical equality (e.g., checking if `$my_num` equals `200`).
Conditional Logic and Refinement
➡️ The `if-else` structure combines two scenarios: executing commands after `then` if the condition is true, or executing commands after `else` if the condition is false.
➡️ The presenter emphasizes efficiency and consistency, urging users to avoid repetition (DRY principle) and unnecessary code lines when structuring conditional logic.
➡️ For testing inequality, the operator `-ne` (not equal) is presented as a clearer alternative to negating a check using the exclamation mark (`!`).
File and Command Existence Checks
📂 To check for the presence of a file or directory, use `-f` for files or `-d` for directories within the conditional structure (e.g., `if [ -f ~/my_file ]`).
➕ The `which` command is introduced as a specialized tool to check if an application or binary is present on the system (e.g., `which htop`).
🔧 A practical script example shows checking if a command (`htop`) exists; if not, it uses `sudo apt update && sudo apt install -y htop` to install it, ensuring automation runs without user prompts (`-y` flag).
Key Points & Insights
➡️ For numerical comparisons in Bash conditions, use specific operators like `-eq` (equal) and `-ne` (not equal).
➡️ When checking for file system objects, use `-f` (file) or `-d` (directory) instead of numerical comparison operators.
➡️ For checking command existence, use the `command -v [command_name]` construct, which is more appropriate and efficient than checking the file path directly, especially when aiming for portability across different binary locations.
➡️ When installing packages within scripts on Debian/Ubuntu systems, always include `sudo apt update` chained with `&&` before the install command, and use the `-y` flag to maintain script automation by suppressing prompts.
📸 Video summarized with SummaryTube.com on Jan 21, 2026, 12:02 UTC
Find relevant products on Amazon related to this video
As an Amazon Associate, we earn from qualifying purchases
Full video URL: youtube.com/watch?v=YrE1Qg-Aw0Q
Duration: 31:36

Summarize youtube video with AI directly from any YouTube video page. Save Time.
Install our free Chrome extension. Get expert level summaries with one click.