0

I've been tasked with refactoring a bash script, and I've encountered the following statement...

if [[ -n "${myvar:-n}" && "${myvar}" == "Y" ]]
then
    ....

I understand that this is checking if the variable is not null and equals "Y", but I'm not sure what the ":-n" is for.

From the testing I've done, it doesn't seem to actually do anything, but I assume it must have been put there for some purpose.

I haven't been able to find any documentation mentioning this.

user1751825
  • 4,029
  • 1
  • 28
  • 58
  • Seems kind of pointless. If the expansion of `myvar` would be the empty string (either because it's not set or it's value is the empty string), then `n` is produced instead. That means `-n` will *always* be true, so I'm not sure why the author bothered. – chepner May 06 '22 at 01:26
  • @chepner Yes, I agree. Now that I understand what this syntax actually does, I can see that it serves no purpose. – user1751825 May 06 '22 at 02:35

0 Answers0