Bash Check Syntax Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with
Bash Syntax Error Near Unexpected Token Done'
us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack bash syntax error near unexpected token else' Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign
Bash Syntax Error Near Unexpected Token Fi'
up How do I check syntax in bash without running the script? up vote 165 down vote favorite 52 Is it possible to check a bash script syntax without executing it? Using Perl, I can run perl -c 'script name'. Is there bash syntax error invalid arithmetic operator any equivalent command for bash scripts? linux bash unix syntax gnu share|improve this question edited May 3 '12 at 16:57 Rob Hruska 69.4k21134171 asked Oct 5 '08 at 12:51 Tom Feiner 6,176154049 Related: Is there a static analysis tool like Lint or Perl::Critic for shell scripts? –blong Jun 3 '14 at 20:47 add a comment| 6 Answers 6 active oldest votes up vote 249 down vote accepted bash -n scriptname Perhaps an obvious caveat: this validates syntax but won't check if your bash syntax error near unexpected token then' bash script tries to execute a command that isn't in your path, like ech hello instead of echo hello. share|improve this answer edited Nov 15 '12 at 15:47 Chris 19.2k57396 answered Oct 5 '08 at 12:55 andy 4,49011217 7 In bash's manpage, under "SHELL BUILTIN COMMANDS / set", -n is documented, and as the beginning of the manpage states, bash interprets all single-character options that set does. –ephemient Oct 5 '08 at 20:55 17 to add to the (for me) non obvious caveat, it also won't catch an error caused by a missing space if ["$var" == "string" ] instead of if [ "$var" == "string" ] –Brynjar Aug 5 '11 at 16:13 9 @Brynjar That's because it's just syntax checking. The open bracket isn't syntax, that's the name of the function to run. type [ says "[ is a shell builtin". It ultimately delegates to the test program, but it expects a closing bracket, as well. So it's like if test"$var", which isn't what the author meant, but is syntactically valid (say $var has a value of "a", then we will see "bash: testa: command not found"). Point is that syntactically, there is no missing space. –Joshua Cheek Jul 20 '14 at 4:30 2 @JoshuaCheek: Builtin [ is only invoked in this case if $var happens to expand to an empty string. If $var expands to a non-empty string, [ is concatenated with that string and is interpreted as a command name (not function
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about
Bash Syntax Error Near Unexpected Token Do'
hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered bash syntax error near unexpected token newline' Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.
Bash Syntax Error Unexpected End Of File
Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Debugging shell scripts: Syntax checking without executing up http://stackoverflow.com/questions/171924/how-do-i-check-syntax-in-bash-without-running-the-script vote 7 down vote favorite Is there some option I can give to just check the syntax of a [bash] shell script to check the syntax of it, but not actually execute anything nor cause any potential damage? bash shell-script testing share|improve this question edited May 25 '11 at 20:40 asked May 25 '11 at 20:34 Aeyoun 483720 add a comment| 1 Answer 1 active oldest votes up vote 7 down vote accepted From the bash(1) man page: -n Read http://unix.stackexchange.com/questions/13790/debugging-shell-scripts-syntax-checking-without-executing commands but do not execute them. This may be used to check a shell script for syntax errors. This is ignored by interactive shells. share|improve this answer answered May 25 '11 at 20:56 Michael Mrozek♦ 44.9k19144179 Obviously. I don’t know why I was so blind that I couldn’t see this when I was through the manual pages. –Aeyoun May 25 '11 at 21:34 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged bash shell-script testing or ask your own question. asked 5 years ago viewed 6405 times active 5 years ago Related 0Shell script executing in the terminal but not from shell script file14How to make bash abort the execution of a script on syntax error?2Bash shell scripting basic question regarding the syntax and basename0Shell script testing through terminal simulation3syntax error in conditional expression while executing the shell script?1Check that only root can edit a script before executing it9Check bash/shell script dependencies3Is it possible to emulate older versions of Bash?0Testing using shell scripts Hot Network Questions A name for a well-informed person who is not believed? How co
Add comments May 302010 How to perform syntax check on a bash script? Answer: You can perform syntax check on a bash http://www.linuxask.com/questions/how-to-perform-syntax-check-on-a-bash-script script, without actually running it using the following command: # bash -n script.sh But if your script contain execution of other program, bash will not try to run it, even if http://wiki.bash-hackers.org/scripting/debuggingtips it does not exist, error of this type will not be returned. Related posts: How to perform syntax check for Python program How to perform syntax check for Perl program Perform syntax syntax error check to PHP files Check if a file exist in Bash Shell How to ignore error in Bash script Posted by Linux Ask! at 2:47 pm Tagged with: Bash Leave a Reply Cancel reply Name (required) E-mail (required) URI Your Comment You may use these HTML tags and attributes:
bash syntax error
How to unset an environment variable in bash Multi-line string in Bash Search Categories Advanced Linux (132) Amazon Web Services (1) EC2 (1) Applications (75) Accessories (17) Graphics (4) Internet (43) Office (3) Sound & Video (7) Basic Linux (382) Commands (329) Databases (103) MongoDB (17) MySQL (84) Redis (2) Desktop Environments (5) GNOME (3) Distribution Specific (83) CentOS (16) Debian (34) Fedora (16) Red Hat (17) Ubuntu (68) Linux Security (31) Other Unix-like Operating Systems (22) Mac OS X (22) Programming (201) Bash (26) C (2) Java (5) NodeJS (3) Perl (65) PHP (58) Python (20) Ruby (9) Revision Control Systems (43) CVS (2) Git (23) Mercurial (8) SVN (11) Servers (51) Apache (35) Nginx (14) Squid (1) Virtualization (5) KVM (1) Xen (3) TagsApache apt-get awk Bash CentOS command curl Debian diff dns Fedora filesystem find git grep ImageMagick java linux mac mac terminal memory usage Mercurial MongoDB MySQL network networking Nginx performance Perl PHP Python redhat rpm Ruby security sed shell smtp ssh svn Ubuntu vi vim wget xargs Subscribe © 2013 Linux Ask! Suffusion theme by Sayontan Sinha
of Contents Debugging a script Use a unique name for your script Read the error messages Use a good editor Write logfiles Inject debugging code Use shell debug output Simple example of how to interpret xtrace output Making xtrace more useful Debugging commands depending on a set variable Dry-run STDIN driven commands Common error messages Unexpected end of file Unexpected end of file while looking for matching ... Too many arguments !": event not found syntax error near unexpected token `(' The CRLF issue What is the CRLF issue? How did a CR end up in my file? Why do CRs hurt? How can I find and eliminate them? See also Discussion Debugging a script These few lines are not intended as a full-fledged debugging tutorial, but as hints and comments about debugging a Bash script. Use a unique name for your script Do not name your script test, for example! Why? test is the name of a UNIX®-command, and most likely built into your shell (it's a built-in in Bash) - so you won't be able to run a script with the name test in a normal way. Don't laugh! This is a classic mistake Read the error messages Many people come into IRC and ask something like "Why does my script fail? I get an error!". And when you ask them what the error message is, they don't even know. Beautiful. Reading and interpreting error messages is 50% of your job as debugger! Error messages actually mean something. At the very least, they can give you hints as to where to start debugging. READ YOUR ERROR MESSAGES! You may ask yourself why is this mentioned as debugging tip? Well, you would be surprised how many shell users ignore the text of error messages! When I find some time, I'll paste 2 or 3 IRC log-snips here, just to show you that annoying fact. Use a good editor Your choice of editor is a matter of personal preference, but one with Bash syntax highlighting is highly recommended! Syntax highlighting helps you see (you guessed it) syntax errors, such as unclosed quotes and braces, typos, etc. From my personal experience, I can suggest vim or GNU emacs. Write logfiles For more complex scripts, it's useful to write to a log file, or to the system log. Nobody can debug your script withou