Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8428193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:59:09+00:00 2026-06-10T04:59:09+00:00

I need help to get the following to work. I need to start a

  • 0

I need help to get the following to work. I need to start a bash process from c++, this bash process needs to accept input from stdin and output as per normal it’s output to stdout.

From a different process I need to write commands to stdin which will then actually execute in bash as per above, then I’m interested in the result from stdout.

This is what I’ve tried so far, but the output does not make sense to me at all…

        if (pipe(pipeBashShell)) {
            fprintf(stderr, "Pipe error!\n");
            exit(1);
        }

        if ((pipePId = fork()) == -1) {
            fprintf(stderr, "Fork error. Exiting.\n"); /* something went wrong */
            exit(1);
        }

        if (pipePId == 0) { //this is the child process
            dup2(pipeBashShell[0], STDIN_FILENO);
            dup2(pipeBashShell[1], STDOUT_FILENO);
            dup2(pipeBashShell[1], STDERR_FILENO);

            static char* bash[] = {"/bin/bash", "-i", NULL};
            if (execv(*bash, bash) == -1) {
                fprintf(stderr, "execv Error!");
                exit(1);
            }
            exit(0);
        } else {
            char buf[512];
            memset(buf, 0x00, sizeof(buf));
            sprintf(buf, "ls\n");
            int byteswritten = write(pipeBashShell[1], buf, strlen(buf));
            int bytesRead = read(pipeBashShell[0], buf, sizeof(buf));

            write(STDOUT_FILENO, buf, strlen(buf));
            exit(0);
        }

.

The output of the result above is as follows:

‘ (main)
bash:: command not found gerhard@gerhard-work-pc:~/workspaces/si/si$ gerhard
orkspaces/si/si$ gerhard@ gerhard-work-pc:~/workspa
….

The command i’m trying to send to bash is “ls”, which should give me a directory listing

Am I missing something here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:59:10+00:00Added an answer on June 10, 2026 at 4:59 am

    You have created one pipe (with two ends) and you are trying to use it for bi-directional communication — from your main process to bash and vice versa. You need two separate pipes for that.

    The way you have connected the file descriptors makes bash talk to itself — it interprets its prompt as a command which it cannot find, and then interprets the error messages as subsequend commands.

    Edit:

    The correct setup works as follows:

    1. prepare two pipes:

      int parent2child[2], child2parent[2];
      pipe(parent2child);
      pipe(child2parent);
      
    2. fork()

    3. in the parent process:

      close(parent2child[0]);
      close(child2parent[1]);
      // write to parent2child[1], read from child2parent[0]
      
    4. in the child process:

      close(parent2child[1]);
      close(child2parent[0]);
      dup2(parent2child[0], STDIN_FILENO);
      dup2(child2parent[1], STDOUT_FILENO);
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help to get a response when I click on an Item from
I need some help to get this right, please. I have created an iPad
I need help regarding JOIN tables to get category name & author name from
I need a little help here: I get a file from an HTML upload
I would need little help here. I'm trying to get the git respository from
I need help with the PHP code for the following: Get the text between
I really need help with using namespaces. How do I get the following code
Need your help to get the max of CAP_PRICE based on certain criteria in
i got some problem and need help .. my plan : 1. get ip
Hi I need a little help: How to get profile URL given an UID?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.