0

I have the following script:

$ cat test.sh
#! /usr/local/bin/bash
date

Run it,

$ test.sh
-bash: test.sh: command not found

It works

$ ./test.sh
Thu Oct 25 18:04:45 CST 2018

Nonetheless, I am aware that test.sh is identical to ./test.sh,

$ file test.sh
test.sh: Bourne-Again shell script text executable, ASCII text
$ file ./test.sh
./test.sh: Bourne-Again shell script text executable, ASCII text

What' the subtle difference between test.sh and ./test.sh?

  • 1
    You can run your script without ./ and from any location if you add directory where the script is stored to $PATH variable. The same is for Windows, it's not Linux/UNIX specific – mrc02_kr Oct 25 '18 at 10:13
  • See also Why do we use “./” to execute a file? – steeldriver Oct 25 '18 at 10:18
1

The two are not identical ways to run a command. With ./test.sh, you run the test.sh script in the current directory. With test.sh, you run the first found test.sh in your $PATH.

Had you called the script test, then running just test would have used the test utility that is likely built into your shell (which does not produce any output), whereas running ./test would have executed your script.

Related:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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