Write and run a real Bash script: the shebang line, three ways to run a script, making it executable, comments, and printing output with echo.
Why: everything you type at the prompt, you can save into a file and run later. A Bash script is a plain text file whose lines are the same commands you already run by hand. Note: create the file with any editor — covered in the Linux course lesson on reading & editing files.
Create a file called hello.sh with one line in it
echo 'echo "Hello from a script"' > hello.shRun it by handing the file to bash
bash hello.shWhy: the first line of a script, starting with #!, tells the system which interpreter should run the file. Without it, whoever runs your script has to know it is Bash. With it, the script declares that itself. Where: it must be the very first line, with no blank line or space before the #.
#!/usr/bin/env bash
# /usr/bin/env finds bash on the user's PATH, so this works
# whether bash lives in /bin or /usr/local/bin.
echo "Hello from a script"Why: with the execute permission set, you can run the script by its path instead of typing "bash" each time. ./ means "in the current directory". Note: chmod and the permission model are covered in the Linux course lesson on permissions & ownership — here you just need +x.
Add the execute permission
chmod +x hello.shRun it directly — the shebang decides the interpreter
./hello.shNote: these are not the same. "bash script.sh" ignores the shebang and the execute bit. "./script.sh" needs both +x and a shebang. "source script.sh" runs the lines in your CURRENT shell, so variables it sets stick around afterward — useful for scripts that configure your session.
1. Explicit interpreter — no +x or shebang needed
bash hello.sh2. Direct execution — needs +x and a shebang
./hello.sh3. Source it — runs in the current shell, keeps its variables
source hello.sh
Comments and echo
Why: anything after a # is ignored by Bash — use comments to explain intent. echo prints a line of text. The -e flag turns on escape sequences like \n (newline) and \t (tab); printf (a later lesson) gives you finer control.