Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Linux & Mac & Bash Command Dictionary

  1. Home
  2. Linux & Mac & Bash Command Dictionary
  3. Creating and Running .sh Files

Creating and Running .sh Files

This page explains how to save shell script code as a text file and run it in the terminal. The file itself is simply a plain text file saved with a .sh extension.

How to write a .sh file

Write your shell script in a text editor and save the file with a .sh extension. Save the file using the UTF-8 character encoding.

It is common practice to place a line called a shebang on the first line of the file. The shebang tells the OS which shell to use when running the script. Writing #!/bin/bash indicates that the script should be run with Bash.

sample_greet.sh
#!/bin/bash

name="username"
echo "Hello, ${name}!"
echo "Today's date: $(date +%Y-%m-%d)"

The shebang on line 1 is optional, but including it explicitly makes the script more portable and less dependent on the environment.

How to write comments

You can write comments (notes) in a .sh file. Comments are ignored by the shell, so they are useful for leaving descriptions or notes about the script.

SyntaxDescription
# commentA single-line comment. Write it with one space after #. Everything from # to the end of the line is a comment.

Bash has no dedicated syntax for multi-line comments. To comment out multiple lines, add # at the beginning of each line.

#!/bin/bash

# Sets the username.
name="example_user"

# Displays a greeting message.
# Use '${variable}' to expand a variable.
echo "Hello, ${name}!"

How to run

Run with the bash command

This is the simplest method. Pass the .sh file to the bash command to run it. No execute permission is required.

bash greet.sh

Difference between sh and bash

You can also run a .sh file with the sh command, but it behaves differently from bash.

sh greet.sh
CommandDescription
bash greet.shExplicitly runs the script with Bash. All Bash-specific features (arrays, [[ ]], $(( )), etc.) are available.
sh greet.shRuns the script with the shell pointed to by /bin/sh. On macOS this is often zsh in POSIX-compatible mode; on Linux it is often a symbolic link to dash or bash.

Because sh runs in POSIX-compatible mode, scripts that use Bash-specific features (such as arrays or [[ ]] conditionals) may produce errors. Scripts with #!/bin/bash in the shebang line should reliably be run with the bash command.

Grant execute permission and run directly

If you grant execute permission to the file, you can run it directly like a command.

First, use the chmod +x command to grant execute permission. This only needs to be done once.

chmod +x greet.sh

After granting permission, run it directly with ./.

./greet.sh
Hello, username!
Today's date: 2024-01-01

./ indicates the current directory. Without it, the shell searches for commands in the directories listed in the PATH environment variable, so ./ is required to run a script in the current directory.

Summary

A .sh file is simply a plain text file. You can create one by writing a shell script in a text editor and saving it with a .sh extension. No special tools are needed.

Shell scripts let you combine multiple commands for automatic execution and incorporate conditional branching and loops. They are useful for automating routine tasks and batch-processing files.

Getting into the habit of writing a shebang line (#!/bin/bash) on the first line of your file makes it clear which shell will be used and prevents differences in behavior across environments.

For information on installing Bash, see Setup.

If you find any errors or copyright issues, please .