Last Error Code Bash
Contents |
Ramey
The exit command terminates a script, just as in a C program. It can also return a value, which is bash if exit code available to the script's parent process.Every command returns an exit status bash return value from function (sometimes referred to as a return status or exit code). A successful command bash script exit on error returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted as an error code. Well-behaved UNIX commands, programs, and utilities return a 0 exit exit bash shell code upon successful completion, though there are some exceptions.Likewise, functions within a script and the script itself return an exit status. The last command executed in the function or script determines the exit status. Within a script, an exit nnn command may be used to deliver an nnn exit status to the shell (nnnBash Not Equal
> must be an integer in the 0 - 255 range).
When a script ends with an exit that has no parameter, the exit status of the script is the exit status of the last command executed in the script (previous to the exit).#!/bin/bash COMMAND_1 . . . COMMAND_LAST # Will exit with status of last command. exitThe equivalent of a bare exit is exit $? or even just omitting the exit.#!/bin/bash COMMAND_1 . . . COMMAND_LAST # Will exit with status of last command. exit $?#!/bin/bash COMMAND1 . . . COMMAND_LAST # Will exit with status of last command.code returned by the last shell command you ran, the incantation to retrieve it in the bash shell is: echo $? Given the nature of this variable (no one indexes
Exit Code 0
text like ‘$?'), it's annoyingly hard to Google for. This entry was posted in get exit code of last command windows Software, Solutions and tagged bash, dollar sign, exit code, exit status, question mark, shell on January 1, 2009 by yiming. bash exit on error Post navigation ← Apple Remote Desktop black screen and old machines Building from source package on Debian / Ubuntu to fix sudo PATH issue → 4 thoughts on “check last exit status code in http://tldp.org/LDP/abs/html/exit-status.html bash shell” Pete March 18, 2010 at 11:55 am Thanks for this! I am porting shell code from OpenVMS to Unix, and was searching for the equivalent to DCL's $status. Reply ↓ TaG November 23, 2012 at 7:41 pm Thanks! Reply ↓ Paul August 22, 2013 at 7:55 am Hey, if you're trying to help people searching for "$?", you should tag the post with the phrase(s) people might http://blog.yimingliu.com/2009/01/01/check-last-exit-status-code-in-bash-shell/ search for. Bash dollar sign question mark! And thanks by the way, this is what I was looking for. Reply ↓ CMS November 22, 2014 at 7:42 am Thanks! Reply ↓ Leave a Reply Cancel reply Your email address will not be published. Required fields are marked *Comment Name * Email * Search Search for: Recent Posts check progress of photoanalysisd Setting up OpenSSH Server on Windows 10 Anniversary Update Not paying OnTheHub to redownload Windows ISOs Python multiprocessing code crashes on OS X under IPython Setting up a Gmail POP3 account for Mail.app on 10.10.3 beta yliu | SelfSolved#132: Photoanalysisd consuming 100% CPU#127: Mail.app stuck at fetching mail for gmail accounts on 10.10.3#124: Exclude /etc/hosts from bash_completion#119: Set execute permission for directories#116: Disable Dell Studio XPS wireless buttonTagsAirPort Apple browser bug Cocoa code compilation dynamic library email email archival email client Entourage error GCC gotcha HTML IMAP information Internet Explorer Javascript libtool Mac Microsoft Microsoft Office networking OpenSSL opinion OS X PHP Projects Python research paper Safari SelfSolved SSH Subversion SVN transcode Ubuntu UI Windows Word workaround XHTML XP Archives September 2016 August 2016 December 2015 July 2015 February 2015 November 2014 February 2013 December 2011 October 2011 August 2011 May
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 http://unix.stackexchange.com/questions/209419/how-to-keep-last-exit-status-after-test hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join http://stackoverflow.com/questions/10448160/in-bash-how-to-set-a-return-code 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 How to keep last exit status after test up exit code vote 7 down vote favorite Is it possible to keep the last command exit status ($?) unaltered after a test? E.g., I would like to do: command -p sudo ... [ $? -ne 1 ] && exit $? The last exit $? should return the sudo exit status, but instead it always returns 0 (the exit code of the test). Is it possible to do that without a temporary variable? Another example to clarify further: spd-say "$@" [ $? -ne exit on error 127 ] && exit $? In this case i want to exit only if the first command is found (exit code != 127). And i want to exit with the actual spd-say exit code (it may not be 0). EDIT: I forgot to mention that i prefer a POSIX-complaint solution for better portability. I use this construct in scripts where i want to provide alternatives for the same command. For instance, see my crc32 script. The problem with temporary variables is that they could shadow other variables, and to avoid that you must use long names, which is not good for code readability. shell-script variable return-status share|improve this question edited Jul 15 '15 at 4:28 Evgeny Vereshchagin 1,8822921 asked Jun 13 '15 at 13:35 eadmaster 351414 No, but you can just do if ! command -p sudo; then exit; fi which would have the same results for your example. –jordanm Jun 13 '15 at 13:44 ok, what if i want to test for the 127 code instead? (eg. [ $? -ne 127 ] && exit $?) –eadmaster Jun 13 '15 at 13:47 1 @jordanm: Really? If the OP has presented the code/logic he meant to, and if I'm reading it correctly, he wants the script to exit if the sudo command succeeds (i.e., if sudo exits with status 0). But, in your code, the script keeps running (doesn't exit) if sudo succeeds –G-M
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 us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up In bash, how to set a return code? up vote 17 down vote favorite 2 I want to set a return value once so it goes into the while loop: #!/bin/bash while [ $? -eq 1 ] do #do something until it returns 0 done In order to get this working I need to set $? = 1 at the beginning, but that doesn't work. bash scripting error-handling return-value share|improve this question asked May 4 '12 at 11:41 JohnnyFromBF 1,77062236 add a comment| 8 Answers 8 active oldest votes up vote 7 down vote accepted #!/bin/bash RC=1 while [ $RC -eq 1 ] do #do something until it returns 0 RC=$? done share|improve this answer answered May 4 '12 at 11:44 Eugen Rieck 42.4k33859 add a comment| Did you find this question interesting? Try our newsletter Sign up for our newsletter and get our top new questions delivered to your inbox (see an example). Subscribed! Success! Please click the link in the confirmation email to activate your subscription. up vote 60 down vote You can set an arbitrary exit code by executing exit with an argument in a subshell. $ (exit 42); echo "$?" 42 So you could do: (exit 1) # or some other value > 0 or use false as others have suggested while (($?)) do # do something until it returns 0 done Or you can emulate a do while loop: while # do some stuff # do some more stuff # do something until it returns 0 do continue # just let the body of the while be a no-op done Either of those guarantee that the loop is run at least one time which I believe is what your goal is. share|improve this answer edited Jul 7 at 15:50 answered May 5 '12 at 0:58 Dennis Williamson 169k43241307 Should be $(exit 42); echo "$?" –Elliot Chance Jul 14 '15 at 4:35 2 @ElliotChance: The dollar sign in my answer represents the prompt and works as-is. What you propose would also work, but isn't necessary. –Dennis Williamson Jul 14 '