← Back to Bash Course | Chapter 1: Setup & Basics | Lesson 7 of 12

Your First Script

Your first script is like writing your first three-step recipe: a label at the top, a few instructions, and notes to remind yourself why. Save it, run it, and watch the computer follow along.

In this page:

  1. Your First Script
Syntax
bash
#!/bin/bash
# describe what the script does
variable_name="value"
echo "message with $variable_name"

Your First Script

A complete first script combines the shebang, a few commands, and comments explaining intent. Saving it with a .sh extension is a convention, not a requirement — Bash doesn't care about file extensions.

What matters is the shebang line and execute permission.

Note: Keep your first scripts small and test them often rather than writing many commands before running anything.

Example: Your First Script

bash
#!/bin/bash
# My first Bash script
echo "Starting script..."
name="World"
echo "Hello, $name!"
echo "Script finished."
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting the #!/bin/bash line at the top of the script.
  2. Forgetting to make the script executable, then getting Permission denied.
  3. Using a relative path inside the script and running it from another directory, so files are not found.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.