Get AI summaries of any video or article — Sign up free
this BASH script will KILL you. thumbnail

this BASH script will KILL you.

NetworkChuck·
5 min read

Based on NetworkChuck's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

Start with an executable Bash script and a shebang, then build interactivity by prompting for input and storing it in variables with `read`.

Briefing

A Bash script can be turned into a playable “Elden Ring”-style mini game by combining user input, randomness, and conditional logic—then layering in cheats and branching rules to make outcomes feel like a real boss fight. The core idea is simple: ask the player for a choice, generate a random “beast” value, and use an `if/else` check to decide whether the player “vanquishes” the boss or “dies.” That decision-making loop is what transforms a one-line script into something interactive.

The build starts with a basic executable shell script (`eldenring.sh`) that prints “you died.” From there, the script gains its first conditional: it prompts the user with “Do you like coffee?” and reads a `coffee` variable. An `if [[ $coffee == "y" ]]` branch prints “you’re awesome,” while an `else` prints “leave right now.” Running the script with `Y` versus `N` immediately changes behavior, demonstrating how Bash conditionals gate different outcomes.

Next comes the game mechanic. For the first boss, the script generates two random numbers between 0 and 1: one for the beast (`beast`) and one for the player’s “attack” (`tarnished`). The win condition is `if [[ $beast == $tarnished ]]`, which yields a 50/50 chance. The script echoes “beast vanquished” on success and “you died” on failure, with `fi` closing the conditional.

The difficulty ramps up for Margit by switching the random range to 0–9, giving a 1-in-10 chance to win. The script also adds `exit 1` when the player loses, forcing a restart—an intentional “you have to try again” loop that mirrors the frustration of hard games.

To make the mini game more “hackable,” the script introduces a cheat code using logical OR (`||`). The player wins if either the random match succeeds or the player types the string `coffee` as the attack value. It then demonstrates logical AND (`&&`) by requiring both conditions to be true (e.g., a numeric comparison plus another check), showing how compound boolean logic changes difficulty and outcomes.

Finally, the script showcases control-flow patterns that scale: nested `if` statements (only root can win) and `elif` chains (alternate winners based on identity, such as a user named Bernard). The last major upgrade adds character classes at the start of the game—samurai, prisoner, or prophet—each with different starting stats (HP, attack, magic). Instead of stacking many `if/elif` blocks, it uses a `case` statement (Bash’s switch-like construct) to assign class-specific parameters cleanly, then prints the chosen class and stats.

By the end, the script isn’t just a toy example—it’s a structured template for interactive command-line games: input → randomness → conditional branching → escalating rules → class-based stats. The takeaway is that Bash can do far more than print text; with conditionals and a bit of math, it can run decision-heavy logic that feels game-like.

Cornell Notes

The script turns a simple Bash file into an “Elden Ring”-style boss battle by using conditionals, randomness, and user input. It starts with an `if/else` coffee prompt, then uses `if [[ $beast == $tarnished ]]` to decide whether the player wins or “dies.” Difficulty increases by changing the random range (50/50 for the first beast, then 1-in-10 for Margit). It adds cheats with `||` (win if the random match OR the user types `coffee`) and demonstrates stricter logic with `&&`. The final upgrade assigns player classes (samurai/prisoner/prophet) using a `case` statement to set HP, attack, and magic cleanly.

How does the script convert user input into branching behavior in Bash?

It prompts the user, stores the response in a variable, then checks that variable with an `if` condition. Example: it asks “Do you like coffee?” and reads into `coffee` using `read coffee`. The conditional uses `if [[ $coffee == "y" ]]` to print “you’re awesome,” and `else` prints “leave right now.” Running with `Y` versus `N` changes which branch executes.

What mechanism creates the boss-fight win/lose randomness?

It generates random integers using arithmetic expansion and modulo, then compares them. For the first beast, it sets `beast` to a random value in {0,1} and asks the player to pick `tarnished` in {0,1}. The win check is `if [[ $beast == $tarnished ]]`, producing a 50/50 outcome. For Margit, it changes the range to {0..9} by using modulo 10, making the win condition `beast == tarnished` a 1-in-10 chance.

How do `||` and `&&` change the meaning of a win condition?

`||` means “OR”: the player wins if either condition is true. The cheat code uses `if [[ $beast == $tarnished ]] || [[ $tarnished == "coffee" ]]` (as implemented in the transcript) so typing `coffee` can override the random mismatch. `&&` means “AND”: both conditions must be true. The transcript demonstrates this by using an AND condition like “47 is greater than 23” combined with another check, so the branch only triggers when every part evaluates true.

What’s the difference between nested `if` and `elif` in the script’s logic?

Nested `if` requires the inner condition to be true after the outer condition succeeds. Example: after the random win check passes, a nested `if` checks whether `user` is `root`; only then does it echo “beast vanquished.” `elif` is an alternate branch: if the first `if` fails, Bash evaluates the `elif` condition instead of requiring both. The transcript uses `elif` to let Bernard win when `user == Bernard`, otherwise it falls to `else` and prints “you died.”

Why use `case` for class selection instead of many `if/elif` blocks?

`case` acts like a switch statement, matching a single variable (`class`) against multiple options (1, 2, 3) and running the corresponding block. The script uses it to assign different starting stats—HP, attack, and magic—based on the chosen class (samurai/prisoner/prophet). It ends each option with `;;` and closes the whole construct with `esac`, keeping the script cleaner than repeated `if/elif` chains.

Review Questions

  1. In the first boss fight, what exact comparison determines victory, and what random range produces the 50/50 chance?
  2. How does the cheat code work using `||`, and what would change if it were replaced with `&&`?
  3. When would a nested `if` prevent a win even if the outer `if` condition is true?

Key Points

  1. 1

    Start with an executable Bash script and a shebang, then build interactivity by prompting for input and storing it in variables with `read`.

  2. 2

    Use `if/else` conditionals with `[[ ... ]]` to branch behavior based on user input (e.g., coffee prompt).

  3. 3

    Implement boss fights by generating random integers with arithmetic expansion and modulo, then compare player and beast values to decide win vs. death.

  4. 4

    Increase difficulty by changing the modulo range (e.g., 0–1 for 50/50, 0–9 for 1-in-10).

  5. 5

    Add “game cheats” by extending win conditions with logical OR (`||`) so alternate inputs can override randomness.

  6. 6

    Use logical AND (`&&`) when multiple conditions must all be true for a branch to run.

  7. 7

    Scale decision logic with nested `if`, `elif` chains, and `case` statements to keep the script maintainable.

Highlights

A 50/50 boss fight is just two random values (0 or 1) plus `if [[ $beast == $tarnished ]]` to choose between “beast vanquished” and “you died.”
Margit’s 1-in-10 chance comes from switching the random range to 0–9 and using the same equality check.
Typing `coffee` can become a cheat win condition when the script uses `||` to allow either a random match or a specific string match.
Nested `if` enables “only root can win,” while `elif` provides clean alternate branches like “Bernard always wins.”
A `case` statement cleanly assigns class-based stats (HP, attack, magic) without stacking many `if/elif` blocks.

Topics

  • Bash Conditionals
  • Random Numbers
  • Game Logic
  • Logical Operators
  • Case Statement