Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Execute commands in git bash using QProcess
Forum Update on Monday, May 27th 2025

Execute commands in git bash using QProcess

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Adithya
    wrote on 22 Sept 2020, 06:50 last edited by Adithya
    #1

    Hi ,
    Here is my snippet.

    QProcess* process = new QProcess(this);
    QString processArgs;

    processArgs ="C:\Program Files\Git\git-bash.exe";

    process->start(processArgs,QStringList());
    if (!process->waitForStarted()) {
    return;
    }
    qDebug()<< process->readAllStandardOutput() <<endl;

    process->write("ls\n\r");
    QByteArray output;
    if (process->waitForReadyRead()) {
    output += process->readAll();
    }
    qDebug() <<"OUTPUT : " << output;

    process->waitForFinished(-1);

    I am trying to open git bash and execute commands from the qt. I am able to open git bash but write is not happening . Is something wrong or is there any alternate way to execute commands .Thanks .....

    J 1 Reply Last reply 22 Sept 2020, 09:12
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 22 Sept 2020, 08:13 last edited by sierdzio
      #2

      Untested, guessing based on how Linux bash works: you should probably call git-bash.exe with -c argument followed by your command or script, like this:

      git-bash.exe -c "ls"
      

      Translated to QProcess lingo, that will be something like:

      process->start("git-bash.exe", {"-c", "'ls'"});
      

      (Z(:^

      1 Reply Last reply
      5
      • A Adithya
        22 Sept 2020, 06:50

        Hi ,
        Here is my snippet.

        QProcess* process = new QProcess(this);
        QString processArgs;

        processArgs ="C:\Program Files\Git\git-bash.exe";

        process->start(processArgs,QStringList());
        if (!process->waitForStarted()) {
        return;
        }
        qDebug()<< process->readAllStandardOutput() <<endl;

        process->write("ls\n\r");
        QByteArray output;
        if (process->waitForReadyRead()) {
        output += process->readAll();
        }
        qDebug() <<"OUTPUT : " << output;

        process->waitForFinished(-1);

        I am trying to open git bash and execute commands from the qt. I am able to open git bash but write is not happening . Is something wrong or is there any alternate way to execute commands .Thanks .....

        J Offline
        J Offline
        JonB
        wrote on 22 Sept 2020, 09:12 last edited by JonB
        #3

        @Adithya
        You do not set any command to execute. That should be C:\Program Files\Git\git-bash.exe (it's not an argument), as @sierdzio shows.

        The simplest is to get it to do the ls and then exit. Similar to @sierdzio (I don't know if his syntax works, and his argument is not quite correct) that would be:

        process->start("C:\\Program Files\\Git\\git-bash.exe", QStringList() << "-c" << "ls"});
        process->waitForFinished(100000);
        auto out = process->readAllStandardOutput();
        auto err = process->readAllStandardError();
        

        If you really want to be able to send (multiple?) commands to a bash via write() your shown code needs changing a touch, but I won't bother unless you want this as bash -c is easier and may actually be what you want?

        You should always put in error checking/read anything from stderr, as these commands sometimes go wrong.

        A 1 Reply Last reply 22 Sept 2020, 09:26
        1
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 22 Sept 2020, 09:15 last edited by
          #4

          Oh right, - in my example was completely unnecessary.

          I don't know if his syntax works

          It does, starting with C++11.

          (Z(:^

          J 1 Reply Last reply 22 Sept 2020, 09:27
          0
          • J JonB
            22 Sept 2020, 09:12

            @Adithya
            You do not set any command to execute. That should be C:\Program Files\Git\git-bash.exe (it's not an argument), as @sierdzio shows.

            The simplest is to get it to do the ls and then exit. Similar to @sierdzio (I don't know if his syntax works, and his argument is not quite correct) that would be:

            process->start("C:\\Program Files\\Git\\git-bash.exe", QStringList() << "-c" << "ls"});
            process->waitForFinished(100000);
            auto out = process->readAllStandardOutput();
            auto err = process->readAllStandardError();
            

            If you really want to be able to send (multiple?) commands to a bash via write() your shown code needs changing a touch, but I won't bother unless you want this as bash -c is easier and may actually be what you want?

            You should always put in error checking/read anything from stderr, as these commands sometimes go wrong.

            A Offline
            A Offline
            Adithya
            wrote on 22 Sept 2020, 09:26 last edited by
            #5

            @JonB Thanks for the help .And yes my intention is to run multiple commands in one session of git bash (That was the reason I was trying via write().) .

            J 1 Reply Last reply 22 Sept 2020, 09:39
            0
            • S sierdzio
              22 Sept 2020, 09:15

              Oh right, - in my example was completely unnecessary.

              I don't know if his syntax works

              It does, starting with C++11.

              J Offline
              J Offline
              JonB
              wrote on 22 Sept 2020, 09:27 last edited by
              #6

              @sierdzio
              Hi.

              It wasn't just the hyphen in front of ls, it's the single-quotes there which I think would/might make it go wrong.

              What I actually meant about your syntax is that I know

              process->start(command, QStringList() << "-c" << "ls"});
              

              works, using QStringList() << . If your shorter {"-c", "ls"} allows compiler to correctly infer overload argument type, I may consider using that in future replies. Does it work?!

              S 1 Reply Last reply 22 Sept 2020, 09:32
              0
              • J JonB
                22 Sept 2020, 09:27

                @sierdzio
                Hi.

                It wasn't just the hyphen in front of ls, it's the single-quotes there which I think would/might make it go wrong.

                What I actually meant about your syntax is that I know

                process->start(command, QStringList() << "-c" << "ls"});
                

                works, using QStringList() << . If your shorter {"-c", "ls"} allows compiler to correctly infer overload argument type, I may consider using that in future replies. Does it work?!

                S Offline
                S Offline
                sierdzio
                Moderators
                wrote on 22 Sept 2020, 09:32 last edited by
                #7

                @JonB said in Execute commands in git bash using QProcess:

                It wasn't just the hyphen in front of ls, it's the single-quotes there which I think would/might make it go wrong.

                Right, I was just trying to make it clear that -c expects a string argument (a script) and not a list of commands. But you are right, Qt will automatically double-quote it before passing it to the process.

                If your shorter {"-c", "ls"} allows compiler to correctly infer overload argument type, I may consider using that in future replies. Does it work?!

                Yes. It's really nothing unusual - C++11 list initialization. I use it regularly for QList, QVector, QHash, QMap.

                (Z(:^

                1 Reply Last reply
                2
                • A Adithya
                  22 Sept 2020, 09:26

                  @JonB Thanks for the help .And yes my intention is to run multiple commands in one session of git bash (That was the reason I was trying via write().) .

                  J Offline
                  J Offline
                  JonB
                  wrote on 22 Sept 2020, 09:39 last edited by JonB
                  #8

                  @Adithya said in Execute commands in git bash using QProcess:

                  And yes my intention is to run multiple commands in one session of git bash (That was the reason I was trying via write().) .

                  Then we need a couple of minor modifications your original code:

                  • process->start("C:\\Program Files\\Git\\git-bash.exe", QStringList());, or process->start("C:/Program Files/Git/git-bash.exe", QStringList());.
                  • No arguments, though you should check the git-bash documentation to see if that wants any special arguments for your case where its input/output is piped.
                  • process->write("ls\r\n");. Your order of the CR-LF is the wrong way round. I don't know whether you need the \r, or even if that will upset it and it wants \n only. You might want a "flush" after each write.
                  • You call process->readAllStandardOutput() immediately after waitForStarted(), that is/could be too early.
                  • You only call waitForReadyRead()/readAll() once. That is (potentially) not enough, output could come back in multiple, separate chunks.
                  • You are relying on the output from the bash/command all being flushed from the other end for you to receive it. You had better hope their implementation does that! If you notice only partial data back to you, this could be the reason.
                  • You are using the ("nasty") blocking/synchronous waitFor...() calls. If you run into trouble this way/things don't behave as you expect, change over to the asynchronous signals/slots methods of QProcess.
                  • Don't forget to close your process/free it!
                  1 Reply Last reply
                  2

                  1/8

                  22 Sept 2020, 06:50

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved