7

I have two cases where I want to create a file with its contents supplied on standard input:

  1. I need to use sudo to have the privileges to create a file.
  2. An application always invokes an (interactive) editor specified by $EDITOR to process this input further, but I want to pipe data from a program instead.

In the former case, I could use echo test | sudo bash -c 'cat > test.txt', but in this as well as in the latter case I usually use tee and direct tee's standard output to /dev/null:

  1. echo test | sudo tee test.txt > /dev/null
  2. echo test | EDITOR=tee application > /dev/null

However, this does not only throw away tee's standard output, but also (in the latter case) application's. It would be preferable to be able to specifically suppress tee's standard output, so that I could instantly notice if something else wrote to standard output.

Is there a utility on standard Linux distributions that will write standard input to the file specified as a command line argument and not output anything?

  • This question sounds like an "XY Problem". What are you really trying to do? – Spiff Aug 11 '15 at 4:22
2

There's the w command of sed:

sed -n 'w /tmp/out'

But perhaps you should just write your own script mycat:

#!/bin/bash
cat >"${1?}"

Create a directory ~/bin and add it to the front of your PATH so your command is found.

You may say, "but I use hundreds of machines, they won't have this script on them". In that case my suggestion is that you create a script that simply sets up your preferred environment on any machine you want. I have a script that will, ideally when I have root access, create a new user id, add a sudoers entry for it, copy some .bashrc and bin/* files, and so on. Then I can login under that id and "feel at home". Also, my command history is then not polluted by other admins doing root logins, and I am not doing all my commands as root.

I often have to work on brand new OS installations on hardware under test, and this sort of automatic setup is a real blessing.

  • 2
    Unfortunately this does not address my question. I'm looking for a standard utility that takes one argument and writes stdin to it. I know how to write wrapper scripts, deploy software, etc.; avoiding that effort is the point of this question. – Tim Landscheidt Dec 2 '16 at 1:06
  • I though sed -n 'w /tmp/out' was pretty cool. – chnrxn Dec 26 '18 at 11:48

Your Answer

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

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