Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 1. Thank you for your response. I actually did have quotes in my script's string but forgot to paste them here. 2. I didn't think it was valid syntax, but I thought maybe I was on the right path by using grep. I don't really understand your script. It does work, so thank you, but I am trying to understand. - First, where are the square [[ ]] brackets for the conditional? - I read the condtional as "If I run this command, it succeeds" Where does it even check else? - How is checking the EXIT STATUS with just a simple conditional? Commented Nov 17, 2022 at 17:54
  • The square brackets aren't required for if, they're used if you want to perform tests on variables or filenames. Every program returns an exit code when run, which can be either 0 (true, success, normal, no problem) or non-zero (false, failure, error). This can be tested directly with if (e.g there's a program called true which always returns true - you can use it in an if statement like if true; then do ... ; fi. and you can use any other program or pipeline of commands in place of true). Commented Nov 17, 2022 at 22:47
  • BTW, [ is a program (see /usr/bin/[), a synonym for test (e.g. /usr/bin/test). Originally, they were only standalone programs but they're used so often in scripts that they're now also built-in to shells for performance. [[ is different, it was never a stand-alone program, it was written to be an enhanced version of [ that can do things that [ can't. It still returns true or false (0 or non-zero). Commented Nov 17, 2022 at 22:53
  • And, finally, the exit code from the last program (or shell built-in command) executed is captured in shell variable $?. This lasts only until another program/command is executed (and then $? is the exit code from THAT program/command), but you can copy it to another variable (e.g. as in my script above: result=$?) if you need it for longer. Commented Nov 17, 2022 at 22:59
  • Thank you for your help. I understand your example now, though I really should find a Bash course so I can learn more. I marked your response as the accepted answer. Commented Nov 28, 2022 at 19:26