Lesson 1 of 8 · 0 of 1 exercises solved
Your First Script
A script is a text file of commands. You will write one, make it executable and run it, and see how the shebang line tells the system which to use.
The shebang (#!) on line 1 tells the OS what program to use to run the file. #!/bin/bash means "run this with Bash." Make the file executable with chmod +x, then run it with ./filename.sh.
#!/bin/bash
# This is a comment
# Above is the "shebang" — tells the OS to use bash
echo "Hello, World!"
echo "My name is $(whoami)" # $() runs a command and inserts its output
echo "Today is $(date +%Y-%m-%d)"
echo "You are in: $PWD" # $PWD is a built-in variable# Save as hello.sh, then:
chmod +x hello.sh # make it executable
./hello.sh # run it
# Or without chmod:
bash hello.sh
# Useful echo options
echo "No newline" -n # -n: no trailing newline
echo -e "Tab:\there" # -e: interpret escape sequences
echo -e "Line1\nLine2" # \n = newline
# printf — more control than echo
printf "Name: %s, Age: %d\n" "Alice" 30
printf "Pi: %.4f\n" 3.14159Practice
Shell exercise
4 checksWrite and run a script
Scripts are just files of commands. Run the existing scripts/hello.sh, then create your own greet.sh in the scripts directory that echoes "Hello from greet", make it executable, and run it.
Practice shell — type help to see what is available.
Finished reading?1 exercise still open. You can come back to it later.
Tip: use the ← and → keys to move between lessons.