Categories
Qt

Qt QProcess “echo” Command Usage

In this short blog post, we will learn how to use “echo” command in Qt application running on Linux OS.

Usually, the easiest way to run terminal commands inside Qt application is using QProcess Class. In Linux world, some commands need specific characters to work with. “echo” command is one of these commands and needs “>” character to create or write inside already created file. These special characters defined and known by “bash” or “sh” scripting languages in the terminal but QProcess Class don’t know what to do with those. Like these situations, there is a simple way to solve this issue.

Create a QString object to pass terminal command but instead of using directly “echo” command use “sh” command to call “echo” command. With proper usage of the escape characters(\), you can use every command which you use on the terminal in your Qt application.

QString testCommand = “/bin/sh -c \”echo ‘Helloworld!’ > helloworld.sh\””;

int ret = QProcess::execute(testCommand);

Thanks for reading.

By Emre Anıl TERZİOĞLU

Embedded Software Engineer

Leave a Reply

Your email address will not be published. Required fields are marked *