You wrote a working bash script.
A bash script is a text file full of the same commands you'd type at a terminal, run all at once. It starts with a shebang line telling Linux which program should run it.
To run it: chmod +x script.sh once, then ./script.sh any time.
Put $ before a variable name to read its value. Double quotes let variables expand inside the text; single quotes do not.
A for loop repeats its body once per item in the list, with the loop variable set to each one in turn.
| Test | Meaning |
|---|---|
| -eq -ne | numbers equal / not equal |
| -gt -lt | greater than / less than |
| == != | strings equal / not equal |
| -z -n | string is empty / not empty |
Functions group commands under a name. When called, $1, $2... refer to the words you pass after the function's name.
script.sh in the editor on the right, one piece at a time. Click Run after each change.echo a line starting with "Hello, ".for loop that echoes the numbers 1 through 5, one per line.$(( )) and an if/else to print "even" or "odd".unlock that, when called with the argument bash, echoes the flag. Call it and submit what it prints.NAME="yourname" then echo "Hello, $NAME" — double quotes let the variable expand.for i in 1 2 3 4 5 then do, your echo $i, then done on its own line.$((NUM % 2)) is 0 for even numbers, 1 for odd. Compare it with -eq 0 inside an if.unlock() { ... }, check if [ "$1" == "bash" ] inside it, then call it on its own line below with unlock bash.| Syntax | Does |
|---|---|
| VAR=value | Assign a variable |
| echo "$VAR text" | Print, expanding variables |
| for v in a b c do ... done | Loop over a list |
| while [ cond ] do ... done | Loop while true (max 500 turns) |
| if [ cond ] then ... else ... fi | Branch on a test |
| name() { ... } | Define a function |
| $1 $2 $# | Function/script arguments |
| $((expr)) | Arithmetic |
| read VAR | Pulls next value from the stdin box |