I use a function called exe() inside some scripts when I want to display the command to be run and then run that command. I wanted to extend this so that, optionally, the person running the script can choose to update the function to also pause on each command at runtime. I can't get this to work. i.e. it prompts for the "y/N" but does nothing, so exe() is not redefined as I would like it during the rest of the script. Can you see what is wrong in this and how to fix it?

# By default, the following 'exe' will run unattended
# If "y" is chosen, 'exe' is altered to display the command before running it
exe() { printf "\n\n"; echo "\$ ${@/eval/}"; "$@"; }
if [ "$(read -e -p 'Step through each configuration option? [y/N]> '; echo $REPLY)" == [Yy]* ]; then
    exe() { printf "\n\n"; echo "\$ ${@/eval/}"; read -p "Any key to continue..."; "$@"; };
  • Re-defining the function you're currently running? Maybe easier to have a global variable that contains the function to call, "exe" initially, "exeprompt" for the "redefined" version... – Hannu Dec 7 '20 at 18:14
  • oh, ok, can you give me a bit more on how to construct that, sorry for not really understanding, but I would love to see how that might look? 🙂 – YorSubs Dec 7 '20 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.