this BASH script will make you a MILLIONAIRE
Based on NetworkChuck's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
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?
What’s the difference between a normal shell variable and an environment variable in this context?
Why edit .bashrc to make variables permanent?
Which built-in system variables are demonstrated, and what do they represent?
How does the script perform math in Bash for the millionaire-age calculation?
What exact formula does the script use to compute the “millionaire age”?
Review Questions
- What range does $random generate by default, and how does random % 15 change that range?
- Why would echo $twitter inside a script fail after setting twitter=Elon Musk without export?
- Where should an exported environment variable be placed to persist across logins, and what file is used in the tutorial?
Key Points
- 1
Create getrichquick.sh, make it executable with chmod +x, and use read or prompts to capture user name and age into variables.
- 2
Use Bash’s built-in $random for pseudo-random integers, but constrain its output with modulo to avoid unrealistic results.
- 3
Perform integer math in Bash with arithmetic expressions using $(( ... )), noting that Bash division defaults to integer behavior (no floating-point).
- 4
Use modulo (%) to generate a specific random range: random % 15 yields offsets from 0 through 14.
- 5
Understand that custom variables set in a shell may not be available to scripts unless exported as environment variables.
- 6
Persist environment variables across sessions by adding export statements to the hidden .bashrc file in the user’s home directory.
- 7
Combine the computed random offset with the user’s age to print a formatted “millionaire age” message.