Which exactly are the system calls we can run via /bin/sh?

I was running the Linux Shell commands in a particular way, I wanted to figure out if both ways of running them would generate the same result.

Example 1:

echo "echo "hi"" | /bin/sh

hi

Example 2:

echo "pwd" | /bin/sh

/root/test

Example 3:

Here, assume that there’s a file called test.txt which only contains the word "test"

echo "cat test.txt" | /bin/sh

test

So far, everything seemed to working fine, but then when I typed the following

echo "cd .." | /bin/sh

Nothing happened, which shouldn’t be the case, because if I type cd .. without | /bin/sh and press Enter, the Shell takes me back to the previous path which in this case would be /root

So, I just want to know what’s the actual list of available commands for use via /bin/sh

>Solution :

You can run any command in this way, but they are executed by the inner shell, and so only have an effect within that shell.

Printing text to the screen using cat, echo or pwd is permanent, even after the inner shell has exited, because actually the inner shell writes to its standard output, and the outer shell (which you are using to type the commands) has linked the inner shell’s output to its own output, which goes to the screen.

The idea of a "current working directory" is not system-wide, but it attached to a process like a shell. The cd command changes the current shell’s working directory, but it does not effect the outer shell’s working directory. Additionally, any new process started by a shell will start with the parent shell’s working directory, but changing directory afterwards in either process will not have an effect on the other.

Environment variables are another thing that only have an effect on the current shell, and you get to choose whether environment variables are passed on to sub-processes by exporting them, or not.

For example, the following will give you the "correct" parent directory:

echo "cd ..; pwd" | /bin/sh

Leave a Reply