1

I just started shell scripting, and I need to check if a file exists so I used command :

    if [[-e "./doc/issues/$1.md" ]]; 

But the command seems to be invalid.

Does anybody knows why ?

1 Answer 1

5

You forgot to put a whitespace before -e, try :

if [[ -e "./doc/issues/$1.md" ]]; 
3
  • 2
    The reason the space is required is that [[ is not mere syntax: it is (essentially) a command, and like every command, it needs a space to separate it from its arguments. Commented May 3, 2019 at 14:23
  • Spaces are critical parts of shell syntax; there are places where they're required (like between [[ and -e -- and also -e and the filename, and the filename and ]]), and places where they're not allowed (like around the = in an assignment), but very few places where they're optional. When copying an example, be sure to copy the spacing along with everything else. Commented May 3, 2019 at 15:00
  • Or in other words: shell scripts work with words. 2 words are usually separated by any number of space, tabs or linefeeds. quotes are used to create words with included spaces/tabs. So [[-e is one word without special meaning. I contrast, [[ -e are 2 words. [[ opens a condition test mode and -e is a condition that accepts the next word as param.
    – Wiimm
    Commented May 4, 2019 at 20:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.