Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Monday, June 4, 2012
How do I check syntax in bash without running the script?
Is it possible to check a bash script syntax without executing it?
Using Perl, I can run perl -c 'script name' . Is there any equivalent command for bash scripts?
(Perhaps an obvious caveat: this validates syntax but won't check if your bash script tries to execute a command that isn't in your path, like ech hello instead of echo hello.)
Run this. If there are any syntax errors in the script, then it returns the same error message. If there are no errors, then it comes out without giving any message. You can check immediately by using echo $?, which will return 0 confirming successful without any mistake.
It worked for me well. I ran on Linux OS, Bash Shell.
bash -n scriptname
ReplyDelete(Perhaps an obvious caveat: this validates syntax but won't check if your bash script tries to execute a command that isn't in your path, like ech hello instead of echo hello.)
sh -n script-name
ReplyDeleteRun this. If there are any syntax errors in the script, then it returns the same error message.
If there are no errors, then it comes out without giving any message. You can check immediately by using echo $?, which will return 0 confirming successful without any mistake.
It worked for me well. I ran on Linux OS, Bash Shell.
I also enable the 'u' option on every bash script I write in order to do some extra checking:
ReplyDeleteset -u
This will report the usage of uninitialized variables, like in the following script 'chek_init.sh'
#!/bin/sh
set -u
message=hello
echo $mesage
Running the script :
$ my_script.sh
Will report the following :
./check_init.sh[4]: mesage: Parameter not set.
Very useful to catch typos