Back to Blog

Your First Shell Script: Shebang, chmod +x & Running Scripts | Zsh Tutorial Lesson 6

Sandy LaneSandy Lane

Video: Your First Shell Script: Shebang, chmod +x & Running Scripts | Zsh Tutorial Lesson 6 by Taught by Celeste AI - AI Coding Coach

Watch full page →

Your First Shell Script: Shebang, chmod +x & Running Scripts

Learn how to create and run your first Zsh shell script on macOS by writing a simple script with a shebang line, making it executable using chmod +x, and running it from the terminal. You'll also see how to add comments for documentation and understand the role of the PATH environment variable in script execution.

Code

#!/bin/zsh
# hello.sh - A simple greeting script
# Author: Your Name
# Date: 2024-06-01
# Purpose: Print a greeting with current date and username

echo "Hello, $USER! Today is $(date)."  # Display greeting with date and user

Key Points

  • The first line #!/bin/zsh (shebang) tells the system to use Zsh to run the script.
  • Create your script file (e.g., hello.sh) and add executable permission with chmod +x hello.sh.
  • Run your script using ./hello.sh to execute it from the current directory.
  • Use comments starting with # to document your script’s purpose, author, and commands.
  • Add a custom scripts folder (e.g., ~/scripts) to your PATH variable to run scripts without ./.