ShakaCode | ShakaCode Blog | Rails On Maui Blog | Rails | ReactJs | JavaScript | Webpack | Productivity |

How to echo commands in a zsh function?


#1

Suppose you want to echo the commands run inside of a zsh function or script. What’s the best way?

setopt verbose

Put this in your script

setopt verbose

Put that wherever you want to start echoing commands as they are run, and when you don’t want that behavior, use

unsetopt verbose

The disadvantage of that one is that you might only want to echo certain commands.

Using zsh functions:

echoRun() {
  echo "> $1"
  eval $1
}

Then I run the command inside a function like this:

foo() {
  echoRun "ls -lt | head"
}