Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More filter arguments with execlp

Tags:

c

shell

I've been trying to play with execlp() and came up with an idea. I want to execute a man command with more filter. Here is what i came up with:

void cmd_help(void)
{
  printf("shell command: help\n");
  execlp("/usr/bin/man", "man", "intro", "| more -8", NULL);
}

It shows the man intro page, but "| more -8" argument is not working. What can be the problem?

like image 810
kulan Avatar asked Jan 28 '26 19:01

kulan


1 Answers

With execlp you can only execute a single binary file and send some arguments to it. Services like I/O redirection or special char expansions will not work, even if they work on command line. This is because they are performed by shell interpreter before the actual command is invoked. To use them, you will need to invoke shell. For example:

void cmd_help(void)
{
  printf("shell command: help\n");
  execlp("/bin/bash", "bash", "-c", "man intro | more -8", NULL);
}
like image 149
Marian Avatar answered Jan 31 '26 13:01

Marian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!