0

Mac 11.4

My current function opens a new terminal, then in the old terminal runs a C program.

foo() {
        open -a Terminal -n; cd ~/desktop/c; ./target.exe
}

What I want it to do is to open a new terminal and run the C program inside the new terminal.

Is this possible to do with a Zsh function?

Ken White
  • 123,280
  • 14
  • 225
  • 444
seamus
  • 2,681
  • 7
  • 26
  • 49
  • Do the answers to [this question](https://stackoverflow.com/questions/7171725/open-new-terminal-tab-from-command-line-mac-os-x) cover what you want to do? – Gordon Davisson Oct 22 '21 at 19:28
  • Does this answer your question? [how to pass command-line arguments to a program run with the open command?](https://stackoverflow.com/questions/29510815/how-to-pass-command-line-arguments-to-a-program-run-with-the-open-command) – user1934428 Oct 25 '21 at 06:34

1 Answers1

1

Save following script in run-command.zsh :

#!/usr/bin/env zsh

run-command(){
    local tmp=$(mktemp) 
    echo "rm $tmp; cd '$PWD'; $*" > $tmp
    chmod 755 $tmp ; open -a Terminal $tmp
}

run-command "$@"

and run it with

chmod +x ./run-command.zsh
./run-command.zsh top

Once you tested "top" successfully, you can run :

./run-command.zsh "cd ~/desktop/c; ./target.exe"
Philippe
  • 20,025
  • 2
  • 23
  • 32