Shell – Can a command be executed without a shell

commandshellssh

I am developing a binary executable program command to execute programs and return the output into a text field.

The parameters to the command involve the command itself as it would be typed on the command line, and the directory. So the routine that executes the operation first switches to the directory, then executes the command.

For example if I want to execute the command some.cmd in the directory /home/user the parameters are command = 'some.cmd' and directory = '/home/user'.

What I have found is that some.cmd does not work but if I change command to /home/user/some.cmd the command works. However the command ls -l works. I also notice that the cd command is not recognized. If I run it remotely via ssh such as setting command to ssh user@localhost 'cd /home/user && ./some.cmd' it works.

It seems that some settings which are present when the command is executed in a shell are not present when it is run directly, but doing it via ssh seems to create the settings for it work.

Is there some explanation for this?

UPDATE:
After some enquiries I got to learn that the API used for executing the commands were not being executed in the shell, or were not executed with the normal environment available from the console. After executing the commands with the /bin/sh -c "cd ..." option the problem is no more. This is the environment doing ssh user@localhost 'command ...' gave me.I am not so sure of the technical details, but apparently the existence of the environment available when you execute in your normal shell is not always available to commands executed directly by the OS.

Best Answer

When you try to execute a file, the system has to know how to find the file. That's why it works if you specify the full path to it. The shell also has a PATH environment variable that stores a list of directories to look in to find an executable. That's why you don't have to specify the full path for ls.

Related Question