Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I execute a Shell built-in command with a C function?

I would like to execute the Linux command "pwd" through a C language function like execv().

The issue is that there isn't an executable file called "pwd" and I'm unable to execute "echo $PWD", since echo is also a built-in command with no executable to be found.

like image 725
user2851770 Avatar asked Oct 06 '13 13:10

user2851770


People also ask

How does the shell execute a built-in function?

Built-in commands, by definition, are executed inside the main executable, as opposed to in a different program. All shell commands are synchronous: the shell waits for the command to complete before it executes the next one.


1 Answers

If you just want to execute the shell command in your c program, you could use,

   #include <stdlib.h>     int system(const char *command); 

In your case,

system("pwd"); 

The issue is that there isn't an executable file called "pwd" and I'm unable to execute "echo $PWD", since echo is also a built-in command with no executable to be found.

What do you mean by this? You should be able to find the mentioned packages in /bin/

sudo find / -executable -name pwd sudo find / -executable -name echo 
like image 180
smRaj Avatar answered Oct 09 '22 02:10

smRaj