0

Following this question on "why do you need ./ (dot-slash) before executable or script name to run it in bash?", I would like to ask if I should specify ./ before the script name even when I specify the program which executes it.

For example, each one of the following works on my system (Windows 10 command-prompt):

  • node test.js
  • node ./test.js
  • node .\test.js
  • python test.py
  • python ./test.py
  • python .\test.py

But I need to provide instructions for my project, and so I would like to have something which is guaranteed to work regardless of the OS in use.

For the sake of this question, please assume that the OS can run the executable program (node and python in the examples above) from any path.

halfer
  • 19,824
  • 17
  • 99
  • 186
goodvibration
  • 5,980
  • 4
  • 28
  • 61
  • 1
    Short answer: No. It might have advantages, but you don't **have to** – oguz ismail Dec 02 '19 at 10:17
  • @oguzismail: Thanks. So it is guaranteed to work on any platform without specifying `./`? – goodvibration Dec 02 '19 at 10:18
  • Yes it is guaranteed, unless the filename starts with a dash – oguz ismail Dec 02 '19 at 10:18
  • _"I would like to have something which is guaranteed to work regardless of the OS in use"_ - in which case avoid `.\test.js`, i.e. a backslash, because node.js on _*nix_ throws: _"Error: Cannot find module"_ – RobC Dec 02 '19 at 10:20
  • @RobC: As you might understand from my question, I am **not using any prefix**, and I would like to know if it's OK to keep it that way. I added those examples just in order to illustrate that on my system everything works (thus implying that I cannot use this info in order to determine whether or not refraining from the "dot-slash" prefix would work on other systems). – goodvibration Dec 02 '19 at 10:24
  • @oguzismail: Thanks. AFAIK, file and folder names cannot contains slashes of any form (only paths can). – goodvibration Dec 02 '19 at 10:25
  • Not slash, a dash, a hyphen, i.e `-` – oguz ismail Dec 02 '19 at 10:36
  • @oguzismail: Oh... sorry, missed that. Thank you very much in that case. You can post that as an answer then. – goodvibration Dec 02 '19 at 10:40
  • @goodvibration : Just for clarificiation, since Your wording _to run it in bash_ is ambiguous. For instance, it makes in general a difference whether you have to use `./myscript.sh` as a command of its own, or `myscript.sh`. The general rule is that whenever a program is looking up a filename using the usual `open` call, a relativ path is always taken relative to the working directory, and this is why your examples with `node ....` and `python ....` do indeed work. – user1934428 Dec 02 '19 at 11:29
  • @goodvibration - Yes, reading your question again does seem like I'm stating the obvious in my previous comment - apologies for that. Given your examples, there is no functional difference between; with `./` or without it - it's a matter of preference. However, as you say; _" I need to provide instructions"_, one could argue that by including `./` it helps to indicatively inform the user their current working directory prior to invoking the `node/python` command should be the same as where `test.(js|py)` resides. – RobC Dec 02 '19 at 11:31
  • @goodvibration - **sidenote:** Given your node example the `.js` suffix is also not necessary. Running `node test` or ``node ./test`` also works. However, in a similar way that `./` is indicative, including the `.js` suffix is indicative too. – RobC Dec 02 '19 at 11:41
  • @RobC: Yes, I'm aware of that, but I want to keep compatibility with other programs (python included). – goodvibration Dec 02 '19 at 11:45

1 Answers1

1

No. It might have advantages on some cases, but you don't always have to prepend the name of a script residing in the current working directory with a ./. If its name starts with a hyphen (-) (or any other character recognized by the interpreter to introduce an option/flag, like a backslash, a plus sign etc.), prepending it with ./ might be required for some interpreters though, but apparently that's not the case here.

So just go with

nodejs test.js
python test.py
oguz ismail
  • 1
  • 16
  • 47
  • 69