22

Consider this very simple bash script:

#!/bin/bash
cat > /tmp/file

It redirects whatever you pipe into it to a file. e.g.

echo "hello" | script.sh

and "hello" will be in the file /tmp/file. This works... but it seems like there should be a native bash way of doing this without using "cat". But I can't figure it out.

NOTE:

  1. It must be in a script. I want the script to operate on the file contents afterwards.

  2. It must be in a file, the steps afterward in my case involve a tool that only reads from a file.

  3. I already have a pretty good way of doing this - its just that it seems like a hack. Is there a native way? Like "/tmp/file < 0 " or "0> /tmp/file". I thought bash would have a native syntax to do this...

4
  • echo "hello" > file
    – karakfa
    Mar 30, 2016 at 15:41
  • 1
    I provided you an answer below, but I don't personally feel that using cat is a hack. You'd use sed or awk if needed? So why not cat? Mar 30, 2016 at 15:56
  • Agree that using cat in this case is not a "hack" if there is no native way to do it. Surprised that it seems that there isn't one. Mar 30, 2016 at 15:58
  • @RafaelBaptista, keep in mind that bash uses NUL-delimited C strings. A file can contain NUL literals, but a shell variable can't. This means that any approach that involves copying content into a shell variable (or some other string internally represented by the shell) will necessarily be lossy, unless implemented in a manner that goes to explicit lengths to be correct in this scenario (ie. using IFS= read -r -d '' string, and checking both exit status and whether the variable is populated to determine whether to print content with a trailing NUL, to print content bare, or to exit). Oct 7, 2017 at 22:01

7 Answers 7

49

You could simply do

cp /dev/stdin  myfile.txt

Terminate your input with Ctrl+D or Ctrl+Z and, viola! You have your file created with text from the stdin.

5
  • 1
    cp is not a built-in in bash. Jan 31, 2019 at 14:42
  • 6
    ...and that's good because it allows you to use the command with other shells.
    – Sebi2020
    Sep 24, 2019 at 11:38
  • Seems /dev/stdin will require root access
    – SiuFay
    Nov 2, 2020 at 11:04
  • 1
    This is awesome! It worked for me on a kubernates pod where vim is not installed.
    – sgowd
    Jul 21, 2022 at 17:53
  • worked well for shoving text into a logfile over Python's paramiko library
    – nmz787
    Feb 28 at 0:29
17
echo "$(</dev/stdin)" > /tmp/file

terminate your input with ENTERctrl+d

3
  • 3
    This will remove any NULs present in the stream. It also will unconditionally add a trailing newline, whether or not one existed in the input. Oct 7, 2017 at 21:55
  • 1
    @CharlesDuffy The command substitution will load all into memory. If there is a lot of data in stdin there might be a problem. Better cat - > /tmp/file
    – Roland
    Nov 6, 2021 at 11:22
  • 1
    @Roland, ah. Yup, your comment is quite correct. Nov 7, 2021 at 19:02
5

I don't think there is a builtin that reads from stdin until EOF, but you can do this:

#!/bin/bash
exec > /tmp/file
while IFS= read -r line; do
  printf '%s\n' "$line"
done
5
  • This will work, and its native in the sense that it doesn't use anything but bash builtins. More awkward than the cat method, since you are reading line by line. Mar 30, 2016 at 16:01
  • 1
    At the very least, you should use IFS= read -r a and printf '%s\n' "$a" to ensure you don't lose any leading or trailing whitespace from a line, or interpret any backslashes as escape sequences.
    – chepner
    Mar 30, 2016 at 16:16
  • 1
    IFS="\n" is putting two characters, the backslash, and n, into IFS. Perhaps you want IFS=$'\n'. Oct 7, 2017 at 21:54
  • And echo $a is going to change a line containing * into a list of files. Oct 7, 2017 at 21:54
  • 1
    ...since this has been accepted answer, and there have been no responses to correctness-related concerns for a rather extended period, I'm stepping over the line a bit here and editing it as necessary to ensure that outputs are identical to inputs (to the extent possible; doesn't handle input with missing trailing newlines or NULs). Oct 7, 2017 at 22:00
3

Another way of doing it using pure BASH:

#!/bin/bash

IFS= read -t 0.01 -r -d '' indata

[[ -n $indata ]] && printf "%s" "$indata" >/tmp/file

IFS= and -d '' causes all of stdin data to be read into a variable indata.

Reason of using -t 0.01: When this script is called with no input pipe then read will timeout after negligible 0.01 seconds delay. If there is any data available in input it will be read in indata variable and it will be redirected to >/tmp/file.

1
  • 3
    If someone runs slow-java-program | yourscript, is it appropriate to ignore any input not promptly delivered (because the input source is far slower to initialize)? Oct 7, 2017 at 21:57
3

Another option: dd of=/tmp/myfile/txt

Note: This is not a built-in, however, it might help other people looking for a simple solution.

1
  • This is a pretty simple way to achieve the basic purpose of piping a STDIN steam into a file. Your answer helps people running into this issue. However, it does not fit the SO's question since dd is a user space utility and not a shell built-in. You should place some notice in your answer when providing alternatives for other visitors. May 16, 2020 at 17:32
0

Why don't you just

GENERATE INPUT | (
    # do whatever you like to the input here
)

But sometimes, especially when you want to complete the input first, then operate on the modified output, you should still use temporary files:

TMPFILE="/tmp/fileA-$$"
GENERATE INPUT | (
    # modify input
) > "$TMPFILE"
(
     # do something with the input from TMPFILE
) < "$TMPFILE"
rm "$TMPFILE"
0
0

If you don't want the program to end after reaching EOF, this might be helpful.

#!/bin/bash
exec < <(tail -F /tmp/a)
cat -

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.