Bash scripting is an effective way to automate repetitive tasks, process data, or manage system operations by running a series of commands in a Bash shell environment, using specific commands and logic to streamline each operation. This guide covers the basic structure of a Bash script, how to create one, how to make it executable, and the different ways to run it. By the end, you'll have several working Bash scripts that you can run directly or through the Bash interpreter.

1. Learn the Bash Script Structure

A Bash script begins with a #!/bin/bash shebang that specifies the Bash interpreter path. The script body consists of executable commands or logic processed line by line — constructs such as variables, loops, arrays, and functions are defined in the body to perform specific tasks. A Bash script doesn't require ending clauses, stop commands, or special terminators such as ; to indicate its end.

A Bash script uses the following structure:

 #!/bin/bash # Commands 

See the Understanding the Bash syntax article for more on the Bash syntax and supported elements.

2. Create a Bash Script

You can create a Bash script as a regular file using any extension, though .sh is commonly used for easier identification. A script needs the correct shebang to run with the Bash interpreter.

1. Create a new basic.sh file:

$ touch basic.sh 

2. List the files in your working directory and verify the script is available:

$ ls -l 

Output:

-rw-r--r-- 1 user user 0 Oct 2 14:08 basic.sh 

The basic.sh file is a regular file with no content — it isn't a valid Bash script until you add a shebang and executable commands.

3. Open the file using a text editor such as nano:

$ nano basic.sh 

4. Add the following contents to the file:

#!/bin/bash echo "Hello World! This is a Bash script" 

Save the file. This script uses the Bash interpreter to display a Hello World message with the echo command.

5. Create another exec-order.sh script:

$ nano exec-order.sh 

6. Add the following contents to the file:

#!/bin/bash echo "This is executed first" echo "This is executed next" echo "This is executed last" 

Save the file. Bash executes every command in the script sequentially, from top to bottom. You can also use conditional statements, variables, or loops to reference previously executed commands or manipulate values for specific tasks.

7. Create another advanced-exec.sh file:

$ nano advanced-exec.sh 

8. Add the following contents to the file:

#!/bin/bash variable1="Hello World" echo "This is executed second" echo "The $variable1 variable was executed first" echo "The last [ $var_last ] variable's value cannot display because it's executed next and not yet in memory" var_last="This is executed last" 

Save the file. Bash runs each command one by one and stores variables in memory as it goes — variable1 is set before the echo commands that reference it run, so its value is already available by the time they execute.

3. Make a Bash Script Executable

A Bash script isn't executable when you first create it — the default permissions mode on Linux is 644, which lets the owner read and write the file while the group and other users can only read it. You need to add the execute (x) permission before you can run the script directly.

1. List the file permissions in your working directory:

$ ls -l 

Output:

total 12 -rw-r--r-- 1 user user 290 Oct 2 14:59 advanced-exec.sh -rw-r--r-- 1 user user 63 Oct 2 14:58 basic.sh -rw-r--r-- 1 user user 119 Oct 2 14:58 exec-order.sh 

Verify that the scripts you created earlier are available and don't yet include the x permission.

2. Enable execute permissions on the basic.sh file:

$ chmod +x basic.sh 

3. Enable execute permissions on the exec-order.sh file:

$ chmod +x exec-order.sh 

4. Enable execute permissions on the advanced-exec.sh file:

$ chmod +x advanced-exec.sh 

5. List the file permissions again and verify execute permissions are active on all scripts:

$ ls -l 

Output:

-rwxr-xr-x 1 user user 290 Oct 2 14:59 advanced-exec.sh -rwxr-xr-x 1 user user 63 Oct 2 14:58 basic.sh -rwxr-xr-x 1 user user 119 Oct 2 14:58 exec-order.sh 

Execute permissions (x) are now active for the file owner, group, and other users, so anyone on the system can run these scripts.

4. Execute a Bash Script

You can run a Bash script as a regular executable or by passing it to the Bash interpreter directly.

1. Execute the basic.sh file as a regular script:

$ ./basic.sh 

Output:

Hello World! This is a Bash script 

2. Execute the exec-order script using the Bash interpreter:

$ bash exec-order 

Output:

This is executed first This is executed next This is executed last 

3. Execute the advanced-exec.sh script:

$ ./advanced-exec.sh 

Output:

This is executed second The Hello World variable was executed first The last [ ] variable's value cannot display because it's executed next and not yet in memory 

Running a script as a regular executable (with ./script.sh) lets other tools, such as automation programs, run its commands directly, while passing it to the bash interpreter works regardless of its executable permission.

Next Steps

  • Add variables, conditionals, and loops to your scripts to handle more complex logic.
  • Package related commands into functions for easier reuse.
  • Explore how to pass command-line arguments into a Bash script.
  • Set up a cron job to run your script on a schedule.

For the full guide with additional tips, visit the original article on Vultr Docs.