Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

2 Answers 2

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.