Server Fault is a question and answer site for system and network administrators. 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

It's not easy to formulate the question properly, maybe it helps when I describe what I'd like to do.

I want to execute a command and pipe it's output into a tool called pastebinit which uploads the STDOUT output to pastebin.

That works very well, however I would like to send the command itself on top of it but w/o typing it a second time.

Is there some command I can launch "my command" with that will

  1. Print "my command" on STDOUT
  2. Executes "my command"

I have the feeling that something like that exists but as hard as it is to formulate such a question properly, I was not able to dig it up with google so far.

share|improve this question
up vote 3 down vote accepted

You can easily write a small wrapper script to do this:


# Start a subshell

# Print the command to standard out
echo "Command: $@"

# Run the command as well

# End the subshell, and pipe all standard output from it to pastebinit
) | pastebinit

Save this file and chmod +x it, then use the script you've created to run the command and pastebinit.

For example:

./ ls -la /root
share|improve this answer
That looks great, will test it right away. I smelled I need a script for it, probably I call it echoexec. – hakre Jun 9 '11 at 18:55
Works like a charm. I've left the pipe after the subshell out so I can pipe to everwhere I want to. Thanks a lot, exactly what I was looking for, any nice that you added comments into the script. – hakre Jun 9 '11 at 19:01
okay, well I didn't test more than ls -l the problem starts with using ' or " for parameters. I assume the shell is removing those. As I execute parameters that need whitespaces and dollar signs preserved in it I think this needs some escaping / handling. However your base script works, I'll read some tutorials. -- update: just "$@" and it works, see… – hakre Jun 9 '11 at 19:47
Ah, good point! :) Glad it helped. – Kyle Smith Jun 9 '11 at 21:23
yeah, thanks for the support. I could not fully solve the problem with this, but I could use it mainly for what I needed it. An older system with the x-server crashed and I wanted to write some docs w/o copying both the commandline and then the output per test. Put it later on my blog. – hakre Jun 9 '11 at 21:36

Try out the command tee. This command reads from an input and then write to a standard output and file.

cat mytext.txt | tee pastebininit

This will send the text of mytext.txt to standard out and to the command pastebininit.

share|improve this answer
I believe the OP wants "cat mytext.txt" to be included in the pastebin, for example. – Kyle Smith Jun 9 '11 at 18:15
Ah, I didn't understand that from the question. – Chris Ting Jun 9 '11 at 18:21
Yeah, it's hard to describe, I knew of something doing what tee does, but didn't remember it's name. So even if not the answer, thanks for the lost name! – hakre Jun 9 '11 at 18:54

A command to run shell commands is sh (the shell), it can be asked to echo the commands to stderr with the -x switch. You can redirect stderr to stdout then.

$ sh -x -c "echo my command" 2>&1
+ echo my command
my command

The side effect of this solution is that the command ('echo my command' in this example) stderr is also sent to stdout. This may be or may not be a problem for you. Also the '+ ' prefix is added to the command.

The '-x' switch will also work for whole scripts, echoing each of the shell commands executed.

share|improve this answer

You can have a look at my answer on another similar question:

I made there a script that can show and execute almost "any" command (ie, the rest of the line is executed "as is", even if it include pipes, complex parameters, quotes, etc. It's not ok for multiline as-is, but could be made so with a few additions, such as a terminating string (to know where the commands to be executed ends), and a little awk script that copy lines from the lineno until the terminating string and put it in the tmp file to be sourced. (see the script for details)

share|improve this answer

Your Answer


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.