TutsFx logo

Bash Scripting Basics

Your First Bash Script

2 min read Last updated 1 hour ago

A Bash script is a plain text file containing commands, with a special first line that tells the system which interpreter to use.

Create your first script

bash
nano hello.sh

Type the following, then save and exit (Ctrl+O, Ctrl+X in nano):

bash
#!/bin/bash

echo "Hello, world!"
echo "Today is $(date +%A)"
  • The first line #!/bin/bash is the shebang — it tells Linux to run this file with the Bash interpreter
  • echo prints text
  • $(...) runs a command and inserts its output — here, date +%A prints the current weekday

Make it executable

A script will not run just because it is a text file. Give it the execute bit you learned about in the permissions tutorial:

bash
chmod +x hello.sh

Run it

bash
./hello.sh
!

`./hello.sh`, not `hello.sh`

The ./ tells Bash to look for the script in the current directory. Your shell only searches its PATH (like /usr/bin) for bare command names — so hello.sh alone will usually fail with "command not found".

Alternative: run it with bash

You can also run any script without making it executable by passing it to Bash directly:

bash
bash hello.sh

Add a comment

# starts a comment — anything after it is ignored:

bash
#!/bin/bash
# This is my first script
echo "Hello, world!"

Try it

  1. Create hello.sh exactly as above
  2. chmod +x hello.sh && ./hello.sh
  3. Change the date command and run it again
Share:

We value your privacy

We use cookies to enhance your browsing experience, serve personalized ads, and analyze traffic. By clicking "Accept", you consent to our use of cookies. Read our Privacy Policy to learn more.