2

I have been going through some online courses on that they asked me to clone a repository from GitHub as follows...

Clone the git repository that contains the artifacts needed for this lab, if it doesn't already exist.

[ ! -d 'cc201' ] && git clone https://gitlab.com/ibm/skills-network/courses/cc201.git

But, I don't understand the below command

[ ! -d 'cc201' ]

what's the use of this?? Can anyone know what exactly does it used for!?

eftshift0
  • 26,375
  • 3
  • 36
  • 60
Raj
  • 80
  • 1
  • 8
  • 1
    Does this answer your question? [How can I check if a directory exists in a Bash shell script?](https://stackoverflow.com/questions/59838/how-can-i-check-if-a-directory-exists-in-a-bash-shell-script) – Jan Wilamowski Jul 08 '21 at 06:18

2 Answers2

4

Note that ! does not have a special meaning in Linux (the operating system), but in several standard programs in Linux. In your case it is not so much a feature of bash (as Mark Bramnik claimed in his otherwise correct answer), but of the standard program called test or [. Since bash emulates this command internally, of course it has to interpret the ! as well, but the definition can be found by doing a man test, where it is described as:

   ! EXPRESSION
         EXPRESSION is false

Actually, bash does have a ! too, with a related, but not identical meaning. If you invoke a program by prefix it with !, i.e.

! prog

bash sets the exit code to 0 if prog terminated with a non-zero exit code, and sets it to 1 if prog terminated with exit code zero. Therefore, you could have written

[ ! -d cc201 ]

equally well as

! [ -d cc201 ]

The overall effect is the same, but the former is the ! from test, which in turn in emulated by bash, while the latter is the builtin ststus-code negator of bash.

user1934428
  • 19,864
  • 7
  • 42
  • 87
3

Exclamation mark is for "Not" (like in boolean), -d flag is for folder exists in bash.

So its essentially a condition, that says: "if the folder named 'cc201' does not exist then do something

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • 3
    It's not in the question by it may be worth mentioning for someone else reading this that `&&` does short-circuit evaluation, so the condition to the left must be true for the condition to the right to be evaluated. – Ted Lyngmo Jul 08 '21 at 06:16