0
#!/bin/bash
if ["$1" == ""]
then
    echo "A"
    echo "B"
fi 

what is the meaning of dollar sign with one number equal double quotation in this case ? ("$1" == "")

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Evo
  • 1
  • 1
  • 4
    Run `help [` at the command line. Do you know what `$1` is? – chepner Oct 17 '21 at 21:01
  • 3
    The code is incorrect, anyway; you *must* have a space after `[` and before `]`. – chepner Oct 17 '21 at 21:02
  • 2
    `man test` or `help test` may also help. – Ted Lyngmo Oct 17 '21 at 21:04
  • @Evo : A square bracket has no special meaning to bash, except that bash implements the `[` command internally (for efficiency perhaps - but the semantics are the same as for the external command), and that `[[` is also an internal bash command. The external `[` command is documented under the man-page for _test_. – user1934428 Oct 18 '21 at 09:30

1 Answers1

1

["$1" == ""], if written correctly as [ "$1" == "" ] or [ "$1" = "" ], in this case tests if the first argument supplied to the script is empty. Thus, the script will print 'A' and 'B' if it is run without arguments (e.g. ./script.sh) or with an empty argument (e.g. ./script.sh "").

Fonic
  • 2,625
  • 23
  • 20
  • 1
    If you want to write it _correctly_ (which is to say, for standards compliance), you'd change `==` to `=`. See https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html – Charles Duffy Oct 17 '21 at 21:42
  • @CharlesDuffy: interesting, though I've never actually seen it written like that in scripts. Even fairly restricted shells like ash have no issues with the `==` syntax. I think it's fair to say the original syntax as not very common these days. I'll add it to my answer anyway. – Fonic Oct 17 '21 at 21:47
  • 1
    Not true that ash reliably allows `==` -- we have Q&A here about it not doing so, though that could possibly be old releases (but I'm certain I've personally seen that behavior inside the last few years). https://stackoverflow.com/questions/46000853/unix-shell-script-unexpected-error/46001203 is an example. – Charles Duffy Oct 17 '21 at 21:54
  • I've been writing scripts specifically for ash for over 10 years now (mostly initramfs stuff) and I've never encountered any problems regarding `==`. That isn't to say that there are ancient versions which might have issues with that syntax. – Fonic Oct 17 '21 at 22:06
  • Maybe your ash version is a symlink to bash? – Jetchisel Oct 17 '21 at 22:25
  • Interesting. Now I'm wondering if it's posh or one of the minimal busybox shells where I've seen that more recently (though with busybox having incorporated ash, the smaller ones aren't exactly recent). – Charles Duffy Oct 18 '21 at 01:31
  • @Jetchisel: now that one made me laugh :) No, certainly not. BUT... (see next comment) – Fonic Oct 18 '21 at 08:27
  • 1
    @Charles Duffy: I had a quick look at ash's history. When I said *ash*, I was talking about *BusyBox's ash*, which I assumed to be the real deal. But BusyBox actually incorporated the *Dash* fork. So we might actually both be right in the end. – Fonic Oct 18 '21 at 08:29