-2

I know there is a way to do this but internet search results always suck with this search -

I want to do:

if [[ ! -z "$var" ]]; then
   # ....
fi

but I can never remember, I think it's like this:

if [[ -p "$var" ]]; then
   # ....
fi

or like this:

if [[ -n "$var" ]]; then
   # ....
fi

can someone just provide a good answer that will show up in search engines, hopefully if I edit the title to be optimal it will help too.

  • [ is test so you can do man test. This will give you what you are looking for. If it did not then you can do man bash | less -r "+/\[\[", then press / Return to find next match (about 3 or 4 times), until you find what you need. – ctrl-alt-delor Mar 04 '20 at 07:32
  • [ "$string" ] or test "$string" is the shortest, nicest and most standard. Do not omit the quotes. And yes, that works even if $string looks like an option. If you have to code for pre-posix shells, use [ "x$string" != x ]. That's the most portable. –  Mar 04 '20 at 10:58
  • 1
    @muru while this certainly has been answered before, it was not in Q/A you link to. When force-closing questions, please make an effort to find the right dupe. –  Mar 04 '20 at 11:00
  • @mosvy I updated what other question this is a dupe of. If you find a better one, add that please. – Kusalananda Mar 04 '20 at 11:30
  • @mosvy no more effort than OP puts in, is my motto. – muru Mar 04 '20 at 11:45
  • 1
    For portability, [[... ]] is not recommended since this is a ksh88 enhancement that intentionally has not been added to the POSIX standard. So better use test of it's alias [. – schily Mar 04 '20 at 13:37
  • @Kusalananda no, that's not much better. I suck at searching -- isn't really any question like "how to test for an empty/null string in the shell?" here? –  Mar 04 '20 at 18:31
  • 1
    @muru it's completely irrelevant who the OP is, how much effort they put in their Q, how much effort they usually put in their Qs, or what their intention are. Closing Qs "strategically" is simply abuse. –  Mar 05 '20 at 08:18
  • @mosvy sure. In any case, I closed the question in good faith. I'd argue that "True if the length of string is (non)zero?" is a pretty close restatement of "Equivalent to check not empty instead of ! -z" - and in most cases the problem would be solved just by reading that question; and if it wasn't, the most common problem is handled in the accepted answer. So maybe put a little more effort into assuming good faith on my part, just maybe? – muru Mar 05 '20 at 08:30
  • @muru We're all human and subject to bias, no need to assume any hard bad faith. I'm simply stumped that there's no simple Q&A handling this; all I could find are about -n or -z, or variations on why [ -n $v ] turns into [ -n ] and succeeds. –  Mar 05 '20 at 08:50

1 Answers1

5

You want the -n test ("true if the length of the given string is non-zero"), which is the opposite of the -z test ("true if the length of the given string is zero").

The -p test is something completely different ("true if the given file exists and is a named pipe").

This information is available to you in an interactive bash shell by typing

help test

which will give you the documentation for the built-in test utility (which [ is another variant of; see help [, and which [[ uses the same tests as; see help [[). You may also have a look at the bash manual on your system (man bash), where this is documented under the section titled "CONDITIONAL EXPRESSIONS".

Your code would therefore be one of

if [[ -n $var ]]; then ...; fi
if [ -n "$var" ]; then ...; fi
if test -n "$var"; then ...; fi

Having said that, I see nothing wrong with

if [[ ! -z $var ]]; then ...; fi

if you, for example, want to make sure that the value is empty and want to output an error message if it isn't empty. The code would then serve as documenting what your intention is: "If this is not empty, there is something wrong". This is a matter of taste.

Kusalananda
  • 333,661