Alexandre Gomes
Alexandre Gomes

Reputation: 392

Programmatically/script to run zsh command

As part of a bigger script I'm using print -z ls to have zsh's input buffer show the ls command. This requires me to manually press enter to actually execute the command. Is there a way to have ZSH execute the command?

To clarify, the objective is to have a command run, keep it in history, and in case another command is running it shouldn't run in parallel or something like that.

Upvotes: 0

Views: 227

Answers (1)

Alexandre Gomes
Alexandre Gomes

Reputation: 392

The solution I've found is:

python -c "import fcntl, sys, termios; fcntl.ioctl(sys.stdin, termios.TIOCSTI, '\n');

I'm not sure why, but sometimes you might need to repeat the command 2 times for the actual command to be executed. In my case this is happening because I send a process to the background, although this still doesn't make much sense because that process is sending a signal back to the original shell (triggering a trap) which actually calls this code.

In case anyone is interested, this was my goal: https://gist.github.com/alexmipego/89c59a5e3abe34faeaee0b07b23b56eb

Upvotes: 1

Related Questions