🎓 A1Learner
🔍
0 of 47 lessons completed

Bash Get Started

A Bash script is a plain text file that holds commands for Bash to run. Saving commands in a file lets the same job run again with one short command.

A script with one line

This code is the whole main.sh file. Running the file prints its single line.

echo "The script ran"

The shebang line comes first

The first line is the shebang and names Bash as the shell. The shebang line itself prints nothing.

#!/bin/bash
echo "Script started"

Lines run top to bottom

Three echo lines sit in the file in order. The output shows the same order, line for line.

#!/bin/bash
echo "Line one"
echo "Line two"
echo "Line three"

Example

#!/bin/bash
echo "Report for today"
echo "Files checked"
echo "Errors found: none"
echo "Report complete"

Key points

  • Script file: a plain text file that holds Bash commands.
  • main.sh: the script file name used in this course.
  • The .sh ending marks a file as a shell script.
  • Run a script by typing bash, a space, then the file name.
  • Shebang: the first line of a script, starting with #!, that names the shell to use.
  • Lines in a script run one by one from top to bottom.

In this example, the shebang line names Bash and prints nothing. The first echo prints the report title. The next two echoes print the check results in order. The last echo prints the closing line.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which command runs the script file main.sh?