Open In App

How to Check the Syntax of a Bash Script Without Running It?

Last Updated : 05 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A bash script is a text file that contains a sequence of commands that are executed by the bash shell, a Unix-based command-line interface. Bash scripts are used to automate tasks, create utility scripts, and perform a wide range of other functions in the command-line environment. They can include variables, loops, conditional statements, and other programming constructs, allowing you to perform a wide range of tasks from the command line.

How to create a Bash Script?

Step 1: Create a text file with .sh file extension. (Here, helloworld.sh).

touch <name of file>.sh

 

Step 2: Open it in an editor like nano, and add the commands you want to execute, one per line.

 

 

Step 3: Save the file.

Ctrl+X -> Y -> Enter key

 

Step 4: Now check for executable permission for the bash script.

ls -l

 

x means that the file has executable permissions.

If it has executable permission, skip Step 5 else continue.

Step 5: Give the file executable permission, using the following command.

chmod +x <name of file with extension>

 

Step 6: Execute the file using bash or ./

bash <name of file>.sh

 

./<name of file>.sh

 

Check for syntax errors

To check the syntax of a Bash script without running it, you can use the bash -n option. This option tells the Bash interpreter to read the script and check the syntax without actually executing the commands in the script.

To use the bash -n (noexec) option, open a terminal and navigate to the directory where the Bash script is located.

cd <directory name>

Then, enter the following command:

bash -n <name of file>.sh

If the script has no syntax errors, this command will not produce any output. 

 

If there are syntax errors, the Bash interpreter will print an error message indicating the line number and the nature of the error.

 

Caveats

  • The “noexec” option only checks the syntax of the script, it does not catch all possible errors. This means that even if the “noexec” option does not report any issues, your script may still contain other mistakes that you need to fix. For example, you may have a runtime error, such as trying to divide by zero, that the “noexec” option will not catch.
  • This does not execute any commands in the script, so it cannot catch runtime errors. This means that if your script has a command that produces an error when it is run, the “noexec” option will not detect this.
  • This does not interpret shell variables, so it may not catch errors related to the use of variables. This means that if you have a typo in a variable name, or if you are using a variable that has not been defined, the “noexec” option will not report this.
  • This option does not check the exit status of commands, so it may not catch errors related to the return value of commands. This means that if you have a command that returns an error code, the “noexec” option will not detect this.
  • This does not check the permissions of files or directories, so it may not catch errors related to file system permissions. This means that if your script tries to access a file or directory that it does not have permission to access, the “noexec” option will not report this.

To check for syntax errors along with the content

To check for syntax errors along with the script’s content, the bash -v option can be used to print each line of the script as it is read by the interpreter, and print the syntax errors in the script if any. This can be helpful for debugging syntax errors or other issues in the script. To use the bash -v option, navigate to the directory where the bash script is located

cd <directory name>

And enter the following command:

bash -v <name of the file>.sh

If no syntax error, then it will just print the lines of the script.

 

If there is a syntax error the Bash interpreter will print an error message indicating the line number and the nature of the error along with the contents of the file.

 

 Using the Shell Check Tool

A static analysis tool for shell scripts is called ShellCheck. It can be applied to bash, sh, and other shell scripts to detect syntax mistakes, semantic flaws, and stylistic problems.

To use Shell Check Tool, it must be installed first:

apt-get install shellcheck

 

To check for errors run the command shellcheck with the name of the file:

shellcheck <name of file>.sh

 

Conclusion

With the bash -n option, you may check for syntax problems. However, keep in mind that this option just examines the script’s syntax and may not identify all potential mistakes. Even so, it’s crucial to thoroughly test your scripts before putting them to use in a real-world setting. You can use the bash -v option to inspect the script’s text and syntax for mistakes. This will output all script lines as they are read by the interpreter, along with any syntax mistakes.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads