1

I'm pretty new to shell programming languages. Why does the following code echo false after printing "File or directory not found."?

#!/bin/sh -xu
ARG_PATH="/srv/path/to/Something"

if ["$ARG_PATH" = "/srv/path/to/Something"] 
then 
   echo "true!" 
else
   echo "false!"
fi

I've tried running the code in sh and bash, doesn't really change anything.

chrisTRD
  • 11
  • 2

2 Answers2

2

check whether path is correct or not ? your syntax is seems okay except Bash is space sensitive give the space after [ and before ]

ARG_PATH="/srv/path/to/Something"

if [ $ARG_PATH = "/srv/path/to/Something" ]
then
        echo $? #display 0 if both r same
fi
Achal
  • 11,821
  • 2
  • 15
  • 37
1

Put spaces around the bracket, if you don't the shell will think ["$ARG_PATH" is the command when it should be [.

The correct test thus is if [ "$ARG_PATH" = "/srv/path/to/Something" ]

Khoyo
  • 1,253
  • 11
  • 20
  • thank you for the quick reply, but that doesn't change anything. I looked this up and read that in Bourne Shell string comparison is done using =, but apparently the shell is trying to evaluate the string as a path or something like that? – chrisTRD Dec 17 '17 at 03:25
  • Note the spaces around the `[` and `]` – Khoyo Dec 17 '17 at 03:27
  • While bash will tolerate `==` inside `[...]`, the [standard POSIX syntax](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html) uses just `=` to compare strings. (This differs from the syntax for the non-POSIX extension `[[...]]`.) – John1024 Dec 17 '17 at 04:00
  • 1
    That's true. Updated my answer to reflect that :) – Khoyo Dec 17 '17 at 04:21
  • `[ ]` is not a syntax element, it is a command. Therefore needs separating spaces. – ULick Dec 17 '17 at 15:12