4

-n noexec ; Read commands and check syntax but do not execute them.


Can you give an example of when we will need "noexec" which is one of the Bash options?

Can someone give me an example of how to properly use this option?

1 Answer 1

6

First, make sure you do not have a file named file in your directory.

Create this syntaxErr.bash:

echo X > file
for i in a b c;
    echo $i >> file
done

As you can see, it misses a do after the for loop. See what happens now:

$ bash -n syntaxErr.bash
syntaxErr.bash: line 4: syntax error near unexpected token `echo'
syntaxErr.bash: line 4: `    echo $i >> file' 
$ cat file
cat: file: No such file or directory
$ bash syntaxErr.bash
syntaxErr.bash: line 4: syntax error near unexpected token `echo'
syntaxErr.bash: line 4: `    echo $i >> file'
$ cat file
X

So, you get the syntax error feedback without actually executing the commands. If you are doing something quite important, you may not want to run the script until all syntax errors have been corrected.

Note: This ctafind.bash does not contain a syntax error:

echo X > file
cta file
find . -type z

cat was misspeled by cta and there is no file of type z for find. Bash does not report the mistakes if you run it with the -n flag.

$ bash -n ctafind.bash 
$ bash ctafind.bash 
ctafind.bash: line 2: cta: command not found
find: Unknown argument to -type: z

After all, Bash can neither know beforehand if there is executable cta nor what are the accepted options of an external command.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .