Outside of a shell, such as running some other process, what does the term current directory mean, and is it possible to execute a binary without specifying the full path or the preceding './', assuming that the current directory contains the executable?

  • "...is it possible to execute a binary without..." You should be more specific about what you want here. If you're using the shell to run commands, the shell has certain rules for how it interprets the command and decides what program to run. If you're writing a program in a programming language, then the language will provide certain ways to run programs. We would have to know what language you're using to priovide detailed help. – Kenster May 21 '15 at 16:20

Yes, it is possible by setting up the search path appropriately (either containing your working directory explicitly or by containing "./"), but it is good practice to have the "./" in front of the program name. The reason is security: A malware could write an executable file with the name of a commonly used program (say, ls) and the next call to ls will execute the local copy instead of /bin/ls. Therefore, standard PATH settings under UNIX do not contain "./".

  • Security wise it would be more enlightening an example with the script receiving a name like sudo or su. That way, any user having its path variable modified will be most probably giving away his password or root's (in case su is used and the user knows root password). – YoMismo May 21 '15 at 14:54

You should better define what is the "other process" and what method it uses to launch a program.

In any case, the current working directory is a property of each and every process running so it might be used to locate a program to run. Whether it is safe to implement it that way is questionable.

  • I am developing the program with Lazarus and the commands are executed with the TProcess object. The directory is set via TProcess.CurrentDirectory and the command via TProcess.CommandLine – vfclists May 21 '15 at 14:02

Well a quick workaround would be creating an alias. This creation can be added to the .bashrc to have it added during startup.

  • An alias is shell specific while the OP specifies "outside of a shell". – jlliagre May 21 '15 at 13:19
  • Oh I read that wrong, my apologies. – Paludan May 21 '15 at 13:21
  • No problem. The OP should really clarify what he means with "outside of a shell". – jlliagre May 21 '15 at 13:36
  • Now I'm just getting thumbed down to the ground xD – Paludan May 21 '15 at 13:36
  • I upvoted your reply to temper that trend ... – jlliagre May 21 '15 at 13:38

The Unix/Linux/*BSD kernel has always kept track of working directory. The chdir() and fchdir() system calls have been around for as long as the C language has been around.

If you write a C language program and use execve(), it's up to your program to specify the filename argument of execve(). The kernel will find executables in the current working directory, or relative to it, if you give a filename argument like "some_executable" or "subdir/some_executable", the kernel will find the executable (if it exists and is executable).

I realizes the modern shells (zsh at least) blur the current working directory a bit so as to be able to cd into a symbolic link, and cd .. back up to the directory containing the symbolic link, but the kernel-based tracking of current working directory is still present. On linux machines you can ls -l /proc/$$/cwd to see exactly what the kernel thinks that directory is named.

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.