3

I'm trying to compare the current working directory's path with my home directory's path.

user@machine:~$ echo $PWD
/home/user
user@machine:~$ if ["$PWD" = "/home/user"]; then echo True; else echo False; fi
bash: [/home/user: No such file or directory
False
user@machine:~$ if ["$PWD" = "whatever"]; then echo True; else echo False; fi
bash: [/home/user: No such file or directory
False

I expected it to print True in the second command and False in the third, while not printing bash: [/home/user: No such file or directory. Why is it printing this and how do I get it to work as I wanted to?

PwS
  • 31
  • 1
  • 2
  • 3
    There must be spaces between `[` or `]` and the string. Try `if [ "$PWD" = "/home/user" ]; then`... It is printing the error message because it tries to find a command `[/home/user` instead of `[` followed by argument `/home/user`. – Bodo Feb 20 '19 at 13:57

1 Answers1

3

you're appending [ to $PWD.

try:

user@machine:~$ if [ "$PWD" = "/home/user" ];
RobotJohnny
  • 1,021
  • 8
  • 18