Get AI summaries of any video or article — Sign up free
this BASH script will make you a MILLIONAIRE thumbnail

this BASH script will make you a MILLIONAIRE

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

Create getrichquick.sh, make it executable with chmod +x, and use read or prompts to capture user name and age into variables.

Briefing

A Bash script can predict—purely for practice—an arbitrary “millionaire age” by combining a built-in random number with a user’s current age. The core mechanism is simple: Bash’s $random generates a pseudo-random integer, and modulo arithmetic reshapes that randomness into a controlled range (0–14). Add that to the user’s age, and the script prints a timestamp-like guess for when the user “will become a millionaire,” turning basic scripting into a hands-on math exercise.

The walkthrough starts by building a beginner-friendly script, getrichquick.sh. It collects user input for name and age, stores each value in variables, and echoes them back in a formatted sentence. From there, the script shifts from input/output to randomness. $random is introduced as a Linux built-in variable that produces a pseudo-random number between 0 and 32,767 each time it’s referenced. To make that randomness usable for the “millionaire” calculation, the script uses arithmetic expressions and modulo (%). Since Bash integer math doesn’t do floating-point by default, modulo becomes the practical tool for constraining ranges.

The tutorial also broadens the variable toolkit. It demonstrates other system variables such as $shell (current shell), $user (current account), $PWD (working directory), and $hostname (machine name), showing that many useful values exist without being set manually. It then covers creating custom variables—like twitter=Elon Musk—but highlights a key gotcha: variables set in a shell session aren’t automatically available to child processes (including scripts). The fix is to export the variable (export twitter=...), turning it into an environment variable that child processes can read.

To make exported variables persist across logins, the script’s author moves the configuration into .bashrc, a hidden file in the user’s home directory that runs on login. After editing .bashrc with an export statement, the variable remains available in new terminal sessions.

With the variable and math foundations in place, the “millionaire age” logic is implemented. The script computes getrich as (random modulo 15) + age, where modulo 15 yields values from 0 through 14. The result is echoed in a sentence like “Name, you will become a millionaire when you are X years old,” preceded by a brief loading message. The final payoff is less about financial prediction and more about learning Bash variables, environment persistence, arithmetic expressions, and modulo-based random range control—skills that are explicitly positioned for the next, more ambitious scripting project.

Cornell Notes

The script predicts a “millionaire age” by adding a controlled random offset to a user’s input age. It first builds a basic Bash program that asks for name and age, stores them in variables, and echoes formatted output. It then uses Bash’s built-in $random (0–32,767) together with modulo arithmetic to restrict randomness to a small range: random % 15 produces 0–14. The tutorial also teaches system variables like $PWD, $user, $hostname, and $shell, plus how to create custom variables and make them available to scripts via export. To persist environment variables across logins, it uses .bashrc.

How does Bash’s $random work, and why does the script need modulo?

$random is a built-in Bash variable that returns a pseudo-random integer between 0 and 32,767 each time it’s referenced. If that value were added directly to age, the result would jump far too high. Modulo arithmetic reshapes the range: using random % 15 yields values from 0 through 14, because modulo returns the remainder after division. That remainder-based range is then added to the user’s age to produce a more reasonable “millionaire age” guess.

What’s the difference between a normal shell variable and an environment variable in this context?

A custom variable set in a shell session (e.g., twitter=Elon Musk) may not be visible inside a script because scripts run as child processes. Exporting the variable (export twitter=...) converts it into an environment variable, making it available to child processes. After exporting, echo $twitter inside the script prints Elon Musk as expected.

Why edit .bashrc to make variables permanent?

Exported environment variables apply to the current session, but they don’t automatically persist after reconnecting or logging in again. .bashrc is executed on login, so adding export twitter='Elon Musk' to .bashrc ensures the variable is set every time a new terminal session starts. The tutorial demonstrates this by exiting the shell, reconnecting, and then successfully echoing $twitter again.

Which built-in system variables are demonstrated, and what do they represent?

The tutorial shows several pre-set variables: $shell for the current shell (Bash), $user for the current user account, $PWD for the current working directory, and $hostname for the machine’s hostname. It also demonstrates echoing these values from within the script without manually setting them.

How does the script perform math in Bash for the millionaire-age calculation?

Bash arithmetic is done using an arithmetic expression syntax: $(( ... )). The tutorial emphasizes that Bash integer math doesn’t use floating-point decimals by default (e.g., 2/3 becomes 0). For the random offset, it computes getrich as $(( (random % 15) + age )), where random % 15 yields 0–14. That integer result is then echoed in a sentence indicating the predicted age.

What exact formula does the script use to compute the “millionaire age”?

It defines getrich as (random modulo 15) + age. In code terms, the random part is constrained with modulo 15 so the offset ranges from 0 to 14, then that offset is added to the user-provided age. The script prints: “Name, you will become a millionaire when you are getrich years old.”

Review Questions

  1. What range does $random generate by default, and how does random % 15 change that range?
  2. Why would echo $twitter inside a script fail after setting twitter=Elon Musk without export?
  3. Where should an exported environment variable be placed to persist across logins, and what file is used in the tutorial?

Key Points

  1. 1

    Create getrichquick.sh, make it executable with chmod +x, and use read or prompts to capture user name and age into variables.

  2. 2

    Use Bash’s built-in $random for pseudo-random integers, but constrain its output with modulo to avoid unrealistic results.

  3. 3

    Perform integer math in Bash with arithmetic expressions using $(( ... )), noting that Bash division defaults to integer behavior (no floating-point).

  4. 4

    Use modulo (%) to generate a specific random range: random % 15 yields offsets from 0 through 14.

  5. 5

    Understand that custom variables set in a shell may not be available to scripts unless exported as environment variables.

  6. 6

    Persist environment variables across sessions by adding export statements to the hidden .bashrc file in the user’s home directory.

  7. 7

    Combine the computed random offset with the user’s age to print a formatted “millionaire age” message.

Highlights

$random produces a pseudo-random integer between 0 and 32,767, but modulo arithmetic is what makes it practical for a small “offset” range.
export is the difference between a variable that exists only in the current shell and one that child processes (scripts) can read.
Editing .bashrc is how exported variables survive new logins without retyping commands.
Bash arithmetic uses $(( ... )) and defaults to integer math, so modulo becomes the go-to tool for range control.
The “millionaire age” calculation is getrich = (random % 15) + age, producing an offset from 0–14.

Topics