I have one program that starts with this line. What does this mean? I am having trouble googling because of the dollar sign.

How come $1 without any parameters? And what does it mean by -d here?

  if [ -d $1 ]; then

And does semi-colon come even if the if-condition does not even start? I thought semi-colon comes only at the end of statement or at the end of condition like

   if () {  };
  • 1
    You might find this useful instead of using Google for such problems: symbolhound.com
    – Chris Down
    Oct 9, 2013 at 7:06

1 Answer 1


The semicolon is required, because without an indication of where that context ends (through a semicolon, newline, etc), if cannot know where the condition ends, and the conditional block begins. Compare:

$ if echo then foo then; then :; fi
then foo then
$ if echo then; then :; fi

-d is a test to check if the next argument is a directory. From help test (because test is equivalent to [):

-d FILE        True if file is a directory.

For example:

$ mkdir foo
$ if [ -d foo ]; then
>     echo foo is a dir
> fi
foo is a dir

$1 is the first argument passed to your program. For example:

$ cat > script << 'EOF'
> #!/bin/sh  
> echo "$1"
$ chmod +x script
$ ./script foo

As an aside, you should quote $1 here, because otherwise it can expand into multiple arguments, resulting in a syntax error from [:

$ dir="foo bar"
$ [ -d $dir ]
sh: 2: [: foo: unexpected operator
$ [ -d "$dir" ]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .