204 β€” Shell Scripting Basics

Beginner

Learn to automate tasks with bash shell scripts. Master variables, conditionals (if/else), loops (for/while), functions, and command-line arguments to create powerful automation scripts.

Learning Objectives

1
Create and execute bash scripts
2
Use variables and command-line arguments
3
Implement conditionals with if/else
4
Write for and while loops
5
Define and call functions
6
Build practical automation scripts
Step 1

Create your first shell script

Write a simple bash script with shebang and basic commands.

Commands to Run

cd ~
mkdir scripting-practice && cd scripting-practice
cat > hello.sh << 'EOF'
#!/bin/bash
echo "Hello, World!"
echo "Today is $(date +%A)"
EOF
cat hello.sh
chmod +x hello.sh
./hello.sh

What This Does

#!/bin/bash is shebang (tells system to use bash). echo prints messages. $(command) runs command and uses output. chmod +x makes script executable. ./ runs script in current directory.

Expected Outcome

Script created with shebang and two echo lines. chmod makes it executable. ./hello.sh prints greeting and current day name.

Pro Tips

  • 1
    #!/bin/bash MUST be first line (shebang)
  • 2
    $(command) executes command and substitutes output
  • 3
    chmod +x required before running script
  • 4
    ./script.sh runs script in current dir
  • 5
    Scripts are just files with commands
  • 6
    Use .sh extension by convention

Common Mistakes to Avoid

  • ⚠️Forgetting shebang (may work but not portable)
  • ⚠️Not making script executable with chmod +x
  • ⚠️Trying to run script.sh instead of ./script.sh
Was this step helpful?

All Steps (0 / 12 completed)