bash
Bash User Handbook
Section titled “Bash User Handbook”Your Complete Guide to Writing Scripts in X
Section titled “Your Complete Guide to Writing Scripts in X”Welcome! This handbook teaches you how to write bash scripts in X, even if you’ve never programmed before. You’ll start with simple scripts and work your way up to powerful automation tools.
Table of Contents
Section titled “Table of Contents”- What Are Bash Scripts?
- Your First Script
- Working with Variables
- Getting User Input
- Making Decisions (If/Else)
- Repeating Actions (Loops)
- Creating Functions
- Working with Lists
- Math Operations
- Working with Files
- Network Operations
- Useful Built-in Functions
- Common Script Examples
- Tips & Tricks
- Troubleshooting
What Are Bash Scripts?
Section titled “What Are Bash Scripts?”Bash scripts are files that contain a series of commands that execute automatically. Instead of typing commands one by one, you write them once in a script and run them anytime!
Why Use Scripts?
Section titled “Why Use Scripts?”- 🚀 Automation: Run complex tasks with one command
- 💾 Reusability: Write once, use many times
- 🎯 Consistency: Do things the same way every time
- ⏱️ Speed: Execute 100 commands instantly
- 🔄 Loops: Repeat actions without repetition
How Scripts Work
Section titled “How Scripts Work”- You write commands in a text file
- Save the file in your bash directory
- Run it with
run scriptname - The script executes all commands automatically
Your First Script
Section titled “Your First Script”Let’s create your very first script!
Hello World
Section titled “Hello World”Create a file called hello in your bash directory:
// My first script!bash_print Hello, World!Run it:
run helloOutput:
Hello, World!🎉 Congratulations! You just wrote and ran your first script!
Simple Greeter
Section titled “Simple Greeter”Now let’s make it interactive:
// Simple greeter scriptbash_print Hello! What's your name?set_var(name, get_string(Your name:))bash_print Nice to meet you, get_var(name)!What happens:
- Prints “Hello! What’s your name?”
- Asks you to type your name
- Stores your name in a variable called
name - Prints a personalized greeting
Try it:
run helloOutput:
Hello! What's your name?Your name: AliceNice to meet you, Alice!Working with Variables
Section titled “Working with Variables”Variables store information you want to use later. Think of them as labeled boxes.
Creating Variables
Section titled “Creating Variables”Use set_var(name, value) to create a variable:
set_var(username, Alice)set_var(age, 25)set_var(score, 98.5)Using Variables
Section titled “Using Variables”Use get_var(name) to get the value back:
bash_print Your name is get_var(username)bash_print You are get_var(age) years oldbash_print Your score: get_var(score)Complete Example
Section titled “Complete Example”// User profile scriptset_var(name, John)set_var(age, 30)set_var(city, New York)
bash_print === USER PROFILE ===bash_print Name: get_var(name)bash_print Age: get_var(age)bash_print City: get_var(city)Output:
=== USER PROFILE ===Name: JohnAge: 30City: New YorkVariable Tips
Section titled “Variable Tips”💡 Tip 1: Use descriptive names
- Good:
username,total_price,user_age - Bad:
x,temp,data
💡 Tip 2: Variables can hold different types
- Text:
set_var(name, Alice) - Numbers:
set_var(count, 42) - Lists:
set_var(items, [apple, banana])
Getting User Input
Section titled “Getting User Input”Make your scripts interactive by asking for input!
Simple Text Input
Section titled “Simple Text Input”set_var(answer, get_string(What's your favorite color?))bash_print Your favorite color is get_var(answer)Number Input
Section titled “Number Input”set_var(age, get_integer(How old are you?))bash_print You are get_var(age) years oldDecimal Number Input
Section titled “Decimal Number Input”set_var(price, get_decimal(Enter price:))bash_print The price is $get_var(price)Complete Input Example
Section titled “Complete Input Example”// User registration scriptbash_print === REGISTRATION ===
set_var(username, get_string(Username:))set_var(email, get_string(Email:))set_var(age, get_integer(Age:))
bash_printbash_print Registration Complete!bash_print Username: get_var(username)bash_print Email: get_var(email)bash_print Age: get_var(age)Making Decisions (If/Else)
Section titled “Making Decisions (If/Else)”Use if statements to make your script do different things based on conditions.
Basic If
Section titled “Basic If”set_var(age, 20)
if get_var(age) >= 18 bash_print You are an adultendifIf with Else
Section titled “If with Else”set_var(score, 75)
if get_var(score) >= 60 bash_print You passed!else bash_print You failedendifIf with Multiple Choices
Section titled “If with Multiple Choices”set_var(score, 85)
if get_var(score) >= 90 bash_print Grade: A - Excellent!elif get_var(score) >= 80 bash_print Grade: B - Good job!elif get_var(score) >= 70 bash_print Grade: C - Passingelse bash_print Grade: F - Need to study moreendifComparison Operators
Section titled “Comparison Operators”Use these to compare values:
==- Equal to!=- Not equal to<- Less than>- Greater than<=- Less than or equal>=- Greater than or equal
Combining Conditions
Section titled “Combining Conditions”Use and and or to combine multiple conditions:
// Checking eligibilityset_var(age, 25)set_var(has_license, 1)
if get_var(age) >= 18 and get_var(has_license) == 1 bash_print You can drive!else bash_print You cannot drive yetendifReal-World Example: Login System
Section titled “Real-World Example: Login System”// Simple login scriptset_var(username, get_string(Username:))set_var(password, get_string(Password:))
if get_var(username) == admin and get_var(password) == secret123 bash_print green(Login successful!) bash_print Welcome back, admin!else bash_print red(Login failed!) bash_print Invalid username or passwordendifRepeating Actions (Loops)
Section titled “Repeating Actions (Loops)”Loops let you repeat actions without writing the same code over and over.
For Loop - Simple List
Section titled “For Loop - Simple List”// Print each fruitfor fruit in [Apple, Banana, Cherry] bash_print I like get_var(fruit)endforOutput:
I like AppleI like BananaI like CherryFor Loop - Counting
Section titled “For Loop - Counting”// Count from 1 to 5for i in range(1, 5) bash_print Count: get_var(i)endforOutput:
Count: 1Count: 2Count: 3Count: 4Count: 5While Loop
Section titled “While Loop”Repeat while a condition is true:
set_var(count, 1)
while get_var(count) <= 5 bash_print Number: get_var(count) set_var(count, get_var(count) + 1)endwhileUntil Loop
Section titled “Until Loop”Repeat until a condition becomes true:
set_var(tries, 0)
until get_var(tries) >= 3 bash_print Attempt: get_var(tries) set_var(tries, get_var(tries) + 1)enduntilReal-World Example: Menu System
Section titled “Real-World Example: Menu System”// Simple menuset_var(running, 1)
while get_var(running) == 1 bash_print bash_print === MENU === bash_print 1. Say Hello bash_print 2. Show Date bash_print 3. Exit
set_var(choice, get_integer(Choose:))
if get_var(choice) == 1 bash_print Hello there! elif get_var(choice) == 2 bash_print Current time: timestamp() elif get_var(choice) == 3 bash_print Goodbye! set_var(running, 0) else bash_print Invalid choice endifendwhileBreaking Out Early
Section titled “Breaking Out Early”Use break to exit a loop:
// Find a numberfor i in range(1, 100) if get_var(i) == 50 bash_print Found it! break endifendforUse continue to skip to the next iteration:
// Print only even numbersfor i in range(1, 10) set_var(remainder, get_var(i) % 2) if get_var(remainder) != 0 continue endif bash_print get_var(i) is evenendforCreating Functions
Section titled “Creating Functions”Functions are reusable blocks of code with a name. Write once, use anywhere!
Simple Function
Section titled “Simple Function”// Define the functionfunc greet bash_print Hello from my function!endfunc
// Use the functiongreetgreetgreetOutput:
Hello from my function!Hello from my function!Hello from my function!Function with Parameters
Section titled “Function with Parameters”// Function that says hello to someonefunc say_hello(name) bash_print Hello, get_var(name)! bash_print Nice to meet you!endfunc
// Use it with different namessay_hello(Alice)say_hello(Bob)say_hello(Charlie)Output:
Hello, Alice!Nice to meet you!Hello, Bob!Nice to meet you!Hello, Charlie!Nice to meet you!Function That Returns a Value
Section titled “Function That Returns a Value”// Calculate areafunc calculate_area(width, height) set_var(area, get_var(width) * get_var(height)) return_value get_var(area)endfunc
// Use the functionset_var(room_area, calculate_area(10, 15))bash_print Room area: get_var(room_area) square feetReal-World Example: Password Validator
Section titled “Real-World Example: Password Validator”// Password validator functionfunc validate_password(password) set_var(length, len(get_var(password)))
if get_var(length) < 8 bash_print red(Password too short!) return_value 0 endif
if get_var(length) > 20 bash_print red(Password too long!) return_value 0 endif
bash_print green(Password valid!) return_value 1endfunc
// Use the validatorset_var(pw, get_string(Enter password:))set_var(is_valid, validate_password(get_var(pw)))
if get_var(is_valid) == 1 bash_print Creating account...else bash_print Please try againendifWorking with Lists
Section titled “Working with Lists”Lists (arrays) store multiple values in one variable.
Creating Lists
Section titled “Creating Lists”// Empty listset_var(items, [])
// List with valuesset_var(fruits, [Apple, Banana, Cherry])set_var(numbers, [1, 2, 3, 4, 5])Adding Items (Push)
Section titled “Adding Items (Push)”Add items to the end of a list:
set_var(cart, [])
push_var cart Laptoppush_var cart Mousepush_var cart Keyboard
// cart now has 3 itemsRemoving from End (Pop)
Section titled “Removing from End (Pop)”set_var(stack, [A, B, C, D])
pop_var stack // Removes Dpop_var stack // Removes C
// stack now has [A, B]Removing from Start (Pull)
Section titled “Removing from Start (Pull)”set_var(queue, [First, Second, Third])
pull_var queue // Removes First
// queue now has [Second, Third]Checking List Size
Section titled “Checking List Size”set_var(items, [A, B, C])len_var items // Returns 3Checking if Value Exists
Section titled “Checking if Value Exists”set_var(colors, [red, green, blue])
in_var colors blue // Returns truein_var colors yellow // Returns falseLooping Through Lists
Section titled “Looping Through Lists”set_var(fruits, [Apple, Banana, Cherry, Orange])
bash_print My favorite fruits:for fruit in fruits bash_print - get_var(fruit)endforReal-World Example: Shopping Cart
Section titled “Real-World Example: Shopping Cart”// Shopping cart managerset_var(cart, [])
bash_print === SHOPPING CART ===
// Add itemspush_var cart Laptopbash_print Added: Laptop
push_var cart Mousebash_print Added: Mouse
push_var cart Keyboardbash_print Added: Keyboard
// Show cartbash_printbash_print Your cart:for item in cart bash_print - get_var(item)endfor
// Show total itemsbash_printbash_print Total items: len(get_var(cart))
// Remove last itempop_var cartbash_printbash_print Removed last itembash_print Items remaining: len(get_var(cart))Math Operations
Section titled “Math Operations”Do calculations in your scripts!
Basic Operations
Section titled “Basic Operations”set_var(a, 10)set_var(b, 3)
// Additionset_var(sum, get_var(a) + get_var(b))bash_print Sum: get_var(sum)
// Subtractionset_var(diff, get_var(a) - get_var(b))bash_print Difference: get_var(diff)
// Multiplicationset_var(product, get_var(a) * get_var(b))bash_print Product: get_var(product)
// Divisionset_var(quotient, get_var(a) / get_var(b))bash_print Quotient: get_var(quotient)
// Remainder (Modulo)set_var(remainder, get_var(a) % get_var(b))bash_print Remainder: get_var(remainder)
// Powerset_var(power, get_var(a) ** 2)bash_print Power: get_var(power)Math Functions
Section titled “Math Functions”// Absolute valueset_var(negative, -42)set_var(positive, abs(get_var(negative)))bash_print Absolute: get_var(positive)
// Roundingset_var(price, 19.99)bash_print Rounded down: floor(get_var(price))bash_print Rounded up: ceil(get_var(price))bash_print Rounded nearest: round(get_var(price))
// Random numberset_var(dice, random(1, 6))bash_print You rolled: get_var(dice)
// Min and Maxbash_print Minimum: min(5, 10)bash_print Maximum: max(5, 10)Real-World Example: Calculator
Section titled “Real-World Example: Calculator”// Simple calculatorbash_print === CALCULATOR ===
set_var(num1, get_decimal(First number:))set_var(num2, get_decimal(Second number:))
bash_printbash_print Results:bash_print Addition: get_var(num1) + get_var(num2) = calc(get_var(num1) + get_var(num2))bash_print Subtraction: get_var(num1) - get_var(num2) = calc(get_var(num1) - get_var(num2))bash_print Multiplication: get_var(num1) * get_var(num2) = calc(get_var(num1) * get_var(num2))bash_print Division: get_var(num1) / get_var(num2) = calc(get_var(num1) / get_var(num2))
// Store the actual calculationsset_var(sum, get_var(num1) + get_var(num2))set_var(diff, get_var(num1) - get_var(num2))set_var(prod, get_var(num1) * get_var(num2))set_var(quot, get_var(num1) / get_var(num2))
bash_printbash_print Sum: get_var(sum)bash_print Difference: get_var(diff)bash_print Product: get_var(prod)bash_print Quotient: get_var(quot)Real-World Example: Shopping Total
Section titled “Real-World Example: Shopping Total”// Calculate shopping total with taxbash_print === SHOPPING TOTAL ===
set_var(price, get_decimal(Item price:))set_var(quantity, get_integer(Quantity:))
set_var(subtotal, get_var(price) * get_var(quantity))set_var(tax, get_var(subtotal) * 0.08)set_var(total, get_var(subtotal) + get_var(tax))
bash_printbash_print Subtotal: $get_var(subtotal)bash_print Tax (8%): $get_var(tax)bash_print Total: $get_var(total)Working with Files
Section titled “Working with Files”Read, write, and manage files in your scripts.
Reading Files
Section titled “Reading Files”// Read entire fileset_var(content, bash_read(/etc/passwd))bash_print get_var(content)Writing Files
Section titled “Writing Files”// Write to a filebash_write myfile.txt Hello, World!
// Write variable contentset_var(message, This is my message)bash_write output.txt get_var(message)Viewing Files
Section titled “Viewing Files”// Display file contentsbash_view /etc/passwdFile Information
Section titled “File Information”// Check if file existsif file_exists(/etc/passwd) bash_print File exists!else bash_print File not foundendif
// Check if it's a folderif is_folder(/home) bash_print It's a directoryelse bash_print It's a fileendif
// Check if it's binaryif is_binary(/bin/program) bash_print Binary fileelse bash_print Text fileendifCreating Directories
Section titled “Creating Directories”bash_mkdir myfolderbash_print Created folder: myfolderNavigation
Section titled “Navigation”// Change directorybash_cd /home/user
// Show current directorybash_pwd
// Show current userbash_print Current user: bash_whoamiFinding Files
Section titled “Finding Files”// Find files by namebash_find /home myfile
// Search file contentsbash_find /home password -c
// Exact filename matchbash_find /var config.txt -eReal-World Example: Log File Analyzer
Section titled “Real-World Example: Log File Analyzer”// Read and analyze a log filebash_print === LOG ANALYZER ===
set_var(logfile, get_string(Log file path:))
if file_exists(get_var(logfile)) set_var(content, bash_read(get_var(logfile))) set_var(size, len(get_var(content)))
bash_print File: get_var(logfile) bash_print Size: get_var(size) characters bash_print bash_print Contents: bash_view get_var(logfile)else bash_print red(Error:) File not found!endifReal-World Example: Backup Script
Section titled “Real-World Example: Backup Script”// Simple backup scriptbash_print === BACKUP TOOL ===
set_var(filename, get_string(File to backup:))
if file_exists(get_var(filename)) // Read the file set_var(content, bash_read(get_var(filename)))
// Create backup with timestamp set_var(backup_name, concat(get_var(filename), .backup)) bash_write get_var(backup_name) get_var(content)
bash_print green(Success!) Backed up to get_var(backup_name)else bash_print red(Error:) File not found!endifNetwork Operations
Section titled “Network Operations”Scan and interact with networks (Grey Hack specific).
Scan for Open Ports
Section titled “Scan for Open Ports”// Scan an IP addressbash_scanner 192.168.1.1List LAN Devices
Section titled “List LAN Devices”// Show all devices on local networkbash_lan_devices 192.168.1.1Show Router Information
Section titled “Show Router Information”// Display router detailsbash_router_info 192.168.1.1Show Device Ports
Section titled “Show Device Ports”// Show ports for a specific devicebash_device_ports 192.168.1.1 192.168.1.10Show Firewall Rules
Section titled “Show Firewall Rules”// Display firewall configurationbash_firewall_rules 192.168.1.1Check if Host is Reachable
Section titled “Check if Host is Reachable”// Ping a hostping_host 192.168.1.1Check if Port is Open
Section titled “Check if Port is Open”// Test specific portport_open 192.168.1.1 22port_open 192.168.1.1 80Real-World Example: Network Scanner
Section titled “Real-World Example: Network Scanner”// Complete network scannerbash_print === NETWORK SCANNER ===
set_var(target, get_string(Target IP:))
bash_print Scanning get_var(target)...bash_print
// Ping firstbash_print Checking if host is reachable...ping_host get_var(target)
// Scan portsbash_printbash_print Scanning for open ports...bash_scanner get_var(target)
// Show router infobash_printbash_print Router information:bash_router_info get_var(target)
// List LAN devicesbash_printbash_print LAN devices:bash_lan_devices get_var(target)
bash_printbash_print Scan complete!Useful Built-in Functions
Section titled “Useful Built-in Functions”String Functions
Section titled “String Functions”// Length of stringset_var(text, Hello)bash_print Length: len(get_var(text))
// Uppercasebash_print Uppercase: upper(get_var(text))
// Lowercasebash_print Lowercase: lower(get_var(text))
// Substringset_var(sub, substr(get_var(text), 0, 3))bash_print Substring: get_var(sub)
// Check if containsif contains(get_var(text), ell) bash_print Found it!endif
// Join list into stringset_var(words, [Hello, World])set_var(sentence, join(get_var(words), " "))bash_print get_var(sentence)
// Split string into listset_var(sentence, Hello World)set_var(words, split_str(get_var(sentence), " "))Type Checking
Section titled “Type Checking”set_var(name, Alice)set_var(age, 25)
bash_print Type of name: typeof(get_var(name))bash_print Type of age: typeof(get_var(age))Type Conversion
Section titled “Type Conversion”// Convert to stringset_var(num, 42)set_var(str, to_string(get_var(num)))
// Convert to integerset_var(text, 123)set_var(number, to_int(get_var(text)))
// Convert to floatset_var(decimal, to_float(3.14))Date and Time
Section titled “Date and Time”// Current timestampbash_print Timestamp: timestamp()
// Current date/timebash_print Date: date()Common Script Examples
Section titled “Common Script Examples”Example 1: User Registration
Section titled “Example 1: User Registration”// User registration systembash_print === USER REGISTRATION ===bash_print
set_var(username, get_string(Username:))set_var(email, get_string(Email:))set_var(age, get_integer(Age:))
// Validationif len(get_var(username)) < 3 bash_print red(Error:) Username too short (min 3 characters) exit 1endif
if get_var(age) < 13 bash_print red(Error:) Must be 13 or older exit 1endif
// Successbash_printbash_print green(Registration successful!)bash_printbash_print Account Details:bash_print Username: get_var(username)bash_print Email: get_var(email)bash_print Age: get_var(age)Example 2: Password Generator
Section titled “Example 2: Password Generator”// Random password generatorbash_print === PASSWORD GENERATOR ===
set_var(length, get_integer(Password length (8-20):))
if get_var(length) < 8 or get_var(length) > 20 bash_print red(Error:) Length must be between 8 and 20 exit 1endif
// Generate password partsset_var(part1, random(1000, 9999))set_var(part2, random(1000, 9999))set_var(part3, random(100, 999))
set_var(password, concat(PASS, get_var(part1), -, get_var(part2), X, get_var(part3)))
bash_printbash_print green(Generated password:)bash_print get_var(password)bash_printbash_print Save this password securely!Example 3: To-Do List
Section titled “Example 3: To-Do List”// Simple to-do listset_var(todos, [])set_var(running, 1)
while get_var(running) == 1 bash_print bash_print === TO-DO LIST === bash_print bash_print 1. Add task bash_print 2. Show tasks bash_print 3. Remove last task bash_print 4. Exit
set_var(choice, get_integer(Choose:))
if get_var(choice) == 1 set_var(task, get_string(Task name:)) push_var todos get_var(task) bash_print green(Task added!)
elif get_var(choice) == 2 bash_print bash_print Your tasks: set_var(count, 0) for task in todos set_var(count, get_var(count) + 1) bash_print get_var(count). get_var(task) endfor
elif get_var(choice) == 3 pop_var todos bash_print yellow(Removed last task)
elif get_var(choice) == 4 bash_print Goodbye! set_var(running, 0)
else bash_print red(Invalid choice) endifendwhileExample 4: Number Guessing Game
Section titled “Example 4: Number Guessing Game”// Number guessing gamebash_print === GUESS THE NUMBER ===bash_print I'm thinking of a number between 1 and 100bash_print
set_var(secret, random(1, 100))set_var(tries, 0)set_var(max_tries, 10)
while get_var(tries) < get_var(max_tries) set_var(tries, get_var(tries) + 1) set_var(remaining, get_var(max_tries) - get_var(tries) + 1)
bash_print bash_print Try get_var(tries) of get_var(max_tries) set_var(guess, get_integer(Your guess:))
if get_var(guess) == get_var(secret) bash_print bash_print green(CORRECT!) You found it in get_var(tries) tries! exit 0 elif get_var(guess) < get_var(secret) bash_print blue(Too low!) Try a higher number else bash_print yellow(Too high!) Try a lower number endif
bash_print Tries remaining: get_var(remaining)endwhile
bash_printbash_print red(Game Over!) The number was get_var(secret)Example 5: System Monitor
Section titled “Example 5: System Monitor”// Simple system monitorbash_print === SYSTEM MONITOR ===bash_print
bash_print Current Directory:bash_pwd
bash_printbash_print Current User:bash_print bash_whoami
bash_printbash_print System Context:bash_print bash_whatami
bash_printbash_print Running Processes:list_processes
bash_printbash_print Press Enter to exitget_string()Tips & Tricks
Section titled “Tips & Tricks”💡 Tip 1: Use Comments Liberally
Section titled “💡 Tip 1: Use Comments Liberally”// This is a comment explaining what the script does// Comments help you remember what each part does later
// Calculate total priceset_var(price, 19.99)set_var(quantity, 3)set_var(total, get_var(price) * get_var(quantity)) // Price times quantity💡 Tip 2: Break Complex Scripts into Functions
Section titled “💡 Tip 2: Break Complex Scripts into Functions”Instead of one long script:
func validate_input(value) // Validation logic here return_value 1endfunc
func process_data(data) // Processing logic here return_value resultendfunc
func display_results(results) // Display logic hereendfunc
// Main scriptvalidate_input(mydata)process_data(mydata)display_results(results)💡 Tip 3: Use Descriptive Variable Names
Section titled “💡 Tip 3: Use Descriptive Variable Names”// Good names (clear and descriptive)set_var(user_age, 25)set_var(total_price, 99.99)set_var(is_valid, 1)
// Bad names (unclear)set_var(x, 25)set_var(temp, 99.99)set_var(flag, 1)💡 Tip 4: Validate User Input
Section titled “💡 Tip 4: Validate User Input”set_var(age, get_integer(Enter your age:))
if get_var(age) < 0 or get_var(age) > 150 bash_print red(Error:) Invalid age! exit 1endif
// Continue with valid agebash_print Your age is get_var(age)💡 Tip 5: Use Colors for Better Output
Section titled “💡 Tip 5: Use Colors for Better Output”bash_print green(Success!) Operation completedbash_print red(Error!) Something went wrongbash_print yellow(Warning:) Please check thisbash_print blue(Info:) Just so you know💡 Tip 6: Test Small Pieces First
Section titled “💡 Tip 6: Test Small Pieces First”Don’t write a huge script all at once! Test small pieces:
// Test 1: Just the inputset_var(name, get_string(Name:))bash_print You entered: get_var(name)
// When that works, add more...💡 Tip 7: Use Sleep for Dramatic Effect
Section titled “💡 Tip 7: Use Sleep for Dramatic Effect”bash_print Preparing to launch...sleep 1bash_print 3...sleep 1bash_print 2...sleep 1bash_print 1...sleep 1bash_print green(LAUNCH!)Troubleshooting
Section titled “Troubleshooting”Problem: Script doesn’t run
Section titled “Problem: Script doesn’t run”Symptoms: Nothing happens when you type run scriptname
Solutions:
- Make sure the script is in your bash directory
- Check that you’re using the correct filename
- Run without any file extension:
run scriptnamenotrun scriptname.txt
Problem: Variable not found
Section titled “Problem: Variable not found”Symptoms: Error like “variable undefined”
Solutions:
- Make sure you created the variable with
set_var - Check spelling - variables are case-sensitive
- Use
get_var(name)to retrieve values
// Wrongset_var(username, Alice)bash_print username // This prints the word "username"
// Rightset_var(username, Alice)bash_print get_var(username) // This prints "Alice"Problem: If statement doesn’t work
Section titled “Problem: If statement doesn’t work”Symptoms: Wrong branch executes or nothing executes
Solutions:
- Don’t forget
endifat the end - Use
get_var()to access variables in conditions - Make sure you’re using the right comparison operator
// Wrongif get_var(age) = 18 // Single = is wrong bash_print Adultendif
// Rightif get_var(age) == 18 // Use == for comparison bash_print AdultendifProblem: Loop never stops
Section titled “Problem: Loop never stops”Symptoms: Script runs forever
Solutions:
- Make sure your condition eventually becomes false
- Check that you’re updating the counter variable
- Use
breakif you need to exit early
// Wrong - infinite loopset_var(count, 0)while get_var(count) < 5 bash_print Count: get_var(count) // Forgot to increment count!endwhile
// Rightset_var(count, 0)while get_var(count) < 5 bash_print Count: get_var(count) set_var(count, get_var(count) + 1) // Increment!endwhileProblem: Math doesn’t work
Section titled “Problem: Math doesn’t work”Symptoms: Numbers add as text instead of adding mathematically
Solutions:
- Make sure you’re using
get_var()for variables - Convert strings to numbers with
to_int()orto_float()
// Wrongset_var(a, "10")set_var(b, "20")set_var(sum, get_var(a) + get_var(b)) // Might concatenate as "1020"
// Rightset_var(a, 10) // Store as number, not stringset_var(b, 20)set_var(sum, get_var(a) + get_var(b)) // Now adds to 30Problem: File not found
Section titled “Problem: File not found”Symptoms: Error when trying to read/write files
Solutions:
- Check the file path is correct
- Use
file_exists()to verify before reading - Use absolute paths (
/home/user/file.txt) if unsure
// Better approachset_var(filename, /etc/passwd)
if file_exists(get_var(filename)) set_var(content, bash_read(get_var(filename))) bash_print get_var(content)else bash_print red(Error:) File not found!endifQuick Reference
Section titled “Quick Reference”Essential Commands
Section titled “Essential Commands”// Variablesset_var(name, value) // Create/update variableget_var(name) // Get variable value
// Inputget_string(prompt) // Get text inputget_integer(prompt) // Get number inputget_decimal(prompt) // Get decimal input
// Outputbash_print message // Print to screen
// Conditionalsif condition // Start if blockelif condition // Else ifelse // Else blockendif // End if block
// Loopsfor var in list // Loop through listfor var in range(start, end) // Loop through rangewhile condition // Loop while trueendfor // End for loopendwhile // End while loop
// Functionsfunc name(params) // Define functionreturn_value value // Return from functionendfunc // End function
// Listspush_var list value // Add to endpop_var list // Remove from endpull_var list // Remove from startlen_var list // Get list size
// Filesbash_read(path) // Read filebash_write(path, content) // Write filebash_view(path) // Display filefile_exists(path) // Check if existsWhat’s Next?
Section titled “What’s Next?”🎓 You’re ready to write scripts!
Start with simple scripts and gradually add more features:
- Start Simple: Begin with input/output scripts
- Add Logic: Learn if/else and loops
- Create Functions: Organize reusable code
- Build Tools: Make useful automation scripts
- Share & Learn: Show your scripts to others!
Need Help?
Section titled “Need Help?”- Look at the examples in this guide
- Start simple and build up gradually
- Don’t be afraid to experiment!
Happy Scripting! 🚀
Remember: Every expert was once a beginner. Start simple, practice often, and you’ll be writing amazing scripts in no time!