How to show OS.execute commands in terminal/powershell? (Linux and Windows)

Nobara Linux, Godot 4.4.1

Hey everyone :slight_smile:

I’m trying to run OS console commands in user and superuser context with Godot. This works well with “OS.execute” so far, but the command is only executed in the background, and I can’t see the progress / result.

OS.execute("bash", ["-c", "mkdir ~/Musik/test"], [], false, true)    # user context
OS.execute("pkexec", ["bash"] + ["-c", "mkdir ~/Musik/test"], [], false, true)  #superuser popup

I would like to see the passed commands executed in the gui console / powershell, so I can watch the progress and react to prompts.

The “open_console: bool” option sounds like it would do just that, but it doesn’t work with Linux (according to the documentation, it only seems to work on Windows). Unfortunately, I only have Linux and macOS at my disposal at the moment, but I would like to know how to do this with Windows as well.

It would be nice if someone could help me with this :slight_smile:

OS.execute_with_pipe() is probably what you want here.

Sadly does not work :sweat_smile:

Does not work how? I’m using it here, on Linux (Gentoo, specifically) and Win10.

Why are you feeding the command through bash? You should be able to:


var stdio = exec_cmd("mkdir", ["~/Musik/test"])

func exec_cmd(cmd: String, args: Array) -> FileAccess:
    var pipes = OS.execute_with_pipe(cmd, args, false)
    return pipes["stdio"]

That should give you a nonblocking FileAccess that can feed stdin and supply stdout, depending on whether you’re reading or writing. Throw something in _process() that sips at the stdio straw, and you should have what you want.

1 Like

There is no standard way to run a command with a visible terminal on Linux. You will need to check for the presence of various terminal emulators in a predetermined order until one of them is found, and call the terminal in question with the correct arguments (the list of arguments is nonstandard).

This is what Godot does to open a terminal with its FileSystem dock context menu action on Linux:

As you can see, it’s quite involved to get right (and required some further changes from its initial implementation). Even then, this implementation is not perfect as it’ll ignore the user’s default terminal preferencess configured in the system settings (which are also nonstandard between GNOME, KDE, Xfce and so on).

Thank you so much for all the answers.

I’ve only been using Linux for a while and can handle terminal tasks without any problems, but I’m no pro.

@hexgrid
I can output the error messages using OS.execute and save them in a variable, but even with your code (at least for me) no visual terminal window opens in which I could see the execution and interact with it.

@Calinou
I’ve been testing for quite a while myself (with my current knowledge), but haven’t found a solution yet. I know that there are many different terminals under Linux. I’m also happy to install another terminal beforehand via command line that can be started with the commands passed to it.

Is there no terminal for Linux that can be started with something like

"terminalxyz -command “mkdir ~/Music/test; mkdir ~/Music/test2; …”

and then display the stuff in the (gui) terminal?

That’s not how Linux works.

What OS.execute_with_pipe() does is give you (as a file you can read and write) the text that is displayed in the terminal, and the input that goes to the terminal.

You can display that text in a window, and send input back. That’s how shells work.

So, what you do is call your command with OS.execute_with_pipe(), and take the stdio FileAccess object you get from the dictionary it returns. Make a RichTextLabel or something, and in its _process() function, read from stdio and append whatever you read to the .text of the label.

If the player types anything that’s supposed to go to the terminal, write that to the stdio object.

If there’s no simple interactive way to do this on Linux, it’s probably too complicated for installing packets etc. this way. As soon as I have some time, I’ll take a closer look at it. Thanks a lot for your reply hexgrid! :slight_smile:

Hi sorry I`m late but you can do this for all OS.

var output = [ ]
OS.execute("CMD.exe", ["/C", "cd %TEMP% && dir"], output)

Then print output.

Here is more information in godot docs.
OS.Execute Docs