Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to print a command (echo it), then run it in a single command ?

This is useful when you have some alias/function and wants to print what the command is before running the command.

This would be the equivalent of

alias mi='echo mvn install; mvn install'

The idea is to avoid the repetition of the command (here 'mvn install' is written twice)

Something like:

alias echoAndRun='...'
alias mi='echoAndRun mvn install'

Thanks for your help.

share|improve this question
up vote 3 down vote accepted

The easiest way to do what you're asking for is probably set -x to turn on command tracing. Remember to turn it off afterward.

function showAndTell {
    set -x
    set +x

(Note that shell aliases are rarely what you want, except in csh derivatives where they can simulate functions to some extent because actual shell functions don't exist.)

Given the above function, you also could simply echo "$@" within the function; if you want to modify how it displays what it's going to run, you would need to do it that way.

share|improve this answer
Echoing "$@" does the trick and was sufficient for my needs. Thanks. – Tom Apr 24 '12 at 12:16

You can use

set -x

before the commands are given.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .