Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check the syntax of a Bash script without running it in Linux?
There are always chances that we will make some type of error whenever we are programming. Compilers and interpreters are always there to help us in these cases, but in order to use them we must run the program or some sort of an IDE that constantly checks for these errors and reminds us every time, so that we can correct them.
What if we don't want to write our code in a fancy IDE and also don't want to run the program either, in that case we are left with very few options if any. In case you are writing a bash script we can certainly check if the bash script has any syntax errors or not.
It should be noted that the syntax errors that we will be able to detect will only be the compile-time errors and not the run-time errors.
Methods to Check Bash Syntax
There are several ways to validate bash script syntax without executing the script −
Using bash -n Command
The -n flag tells bash to read commands but not execute them. This performs syntax checking only.
bash -n script_name.sh
Using sh -n Command
Similar to bash, you can use the sh command with the -n flag −
sh -n script_name.sh
Example − Valid Script
Let's explore an example. Assume we have a bash script which contains a code something like this −
for f in *.txt; do mv "$f" "${f// /_}"; done
The above code is simple, it just loops over all the .txt files present in the current directory and then changes the names of the files which contain spaces between them, as it replaces the spaces with the _(underscores).
This works like a charm, now when we run the command to check whether the above bash script has any syntax error, we won't get any output.
Let's explore this particular case. Run the following command in your terminal, where the sample.sh is also present.
bash -n sample.sh
When I run this command, the terminal will simply print nothing, indicating no syntax errors were found.
Example − Invalid Script
Now let's make a change to our sample.sh which will make it an invalid bash script and then we will run the same command again in the terminal.
for f in *.txt; do mv "$f" "${f// /_}"; done = true
Now when we run the following command shown below −
bash -n sample.sh
The output will be −
immukul@192 linux-questions-code % bash -n sample.sh
sample.sh: line 1: syntax error near unexpected token `='
sample.sh: line 1: `for f in *.txt; do mv "$f" "${f// /_}"; done = true'
That's how we are able to check for syntax errors without even running it.
Limitations − Runtime vs Compile-time Errors
Now, we talked about the fact that this command will only detect the compile-time error and not the runtime error, and we can confirm it by changing the sample.sh again −
for d in *.txt; do mv "$f" "${f// /_}"; done
In the above example, we're using variable d in the loop but referencing undefined variable f in the loop body. This is a logical error that will cause runtime issues.
Now when we run the command shown below on the sample.sh file shown above −
bash -n sample.sh
The output will be nothing, because the syntax is correct even though the logic is flawed.
Additional Options
| Option | Description | Example |
|---|---|---|
| -n | Check syntax without execution | bash -n script.sh |
| -x | Debug mode (shows execution trace) | bash -x script.sh |
| -v | Verbose mode (shows input lines) | bash -v script.sh |
Conclusion
The bash -n command is a useful tool for checking bash script syntax without execution. However, it only catches compile-time syntax errors and cannot detect runtime logical errors or issues with undefined variables. Use this command as a first step in script validation before testing with actual execution.
