1

I'm having a tough time wrapping my soggy little brain around the various usages of [ ], quoted vars and -eq vs ==.

What is the difference between these statements? Is it as simple as one is more posix compliant than the other or is there a functional difference I should worry about?

[ ! -z "$var" ] && echo "Not empty" || echo "Empty"
[[ ! -z "$var" ]] && echo "Not empty" || echo "Empty"

and what about:

if "$report" == "1"; then blah; fi

vs

if [ "$report" == "1" ]; then blah; fi

vs

[ "$report" -eq 1 ]; then blah

I get that [ is a test function in the shell but I don't understand when I need to start with if or when that isn't necessary.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
xlntech
  • 11
  • 1
  • 1
    Certainly all these bits are covered by many different answers in StackOverflow and StackExchange. I don't really think any benefit will be achieved by collecting the pieces together to answer this particular question, even though your questions are valid and we all have faced them when we started shell scripting. – Quasímodo Apr 21 '20 at 17:33
  • Thanks for the .. answer? I was shooting for hearing about good practice rather than just what the syntax of usage would be. – xlntech Apr 21 '20 at 20:45
  • `[` is a command. In bash `[[` is a keyword. `[[` is more likely to behave in ways that you would expect. – jordanm Apr 21 '20 at 20:52
  • 1
    Here's some discussion you can peruse: https://stackoverflow.com/q/669452/10678955 – root Apr 22 '20 at 02:28
  • Thanks @root. That was one I didn't find when I was searching. It's been a slog trying to figure out which ways are "right" in various situations. I've found a thousand examples of usages but it's not so easy to find discussions about why those usages are necessary or if they are better than another. – xlntech Apr 24 '20 at 00:49

0 Answers0