Bash Tutorial · Bash Basics · Lesson 2 of 30

Bash Get Started

Open a terminal, check your Bash version, and write and run your first script with a shebang line.

Create a free account to save this page, take notes and track your progress. Sign in

Open a terminal

  • Linux: open "Terminal"; Bash is usually the default shell.
  • macOS: open Terminal; the default shell is zsh, and bash starts Bash (macOS ships an old 3.2; install a current one with Homebrew: brew install bash).
  • Windows: install WSL (wsl --install) and open Ubuntu.

Check the version with bash --version.

Your first script

Create hello.sh:

#!/usr/bin/env bash
echo "Hello from a script"

The first line, the shebang, tells the system which interpreter runs the file. Make it executable and run it:

chmod +x hello.sh
./hello.sh

The example below creates a script, makes it executable and runs it, all from Bash:

Create, chmod and run a script
cat > hello.sh <<'EOF'
#!/usr/bin/env bash
echo "Hello from a script"
echo "This script is called $0"
EOF
chmod +x hello.sh
./hello.sh
ls -l hello.sh | cut -c1-10

Output

Hello from a script
This script is called ./hello.sh
-rwxrwxr-x

Run this on your computer — see “Get Started” for setup.

./ is needed because the current directory isn't on your PATH (a safety feature).

Test yourself

What does the shebang line #!/usr/bin/env bash do?

  1. Comments out the first line
  2. Tells the system to run the file with bash
  3. Makes the file executable
  4. Imports the bash library
Show answer

Answer: Tells the system to run the file with bash

Without a shebang, ./script runs with whatever shell started it; with it, the right interpreter is always used.

Was this page helpful?