On Tue, Dec 23, 2025 at 14:39:01 +0100, Nicolas George wrote:
> Greg Wooledge (HE12025-12-23):
> > If you're using a temp file in a shell script, typically that temp
> > file is very short-lived. Almost always less than one second.
>
> That is quite an assumption. It is true that most shell scripts are
> very short-lived, but most shell scripts do not require temporary files
> either. Requiring a temporary file makes the script atypical in a way
> that is probably correlated with requiring a long time.
You might be surprised by how many temp files are used by various
programs on a regular basis, then. Historically, bash always used
a temp file for every here document (<<) or here string (<<<). It's
only in the most recent versions that bash started using pipes for
shorter here docs, and falling back to temp files for larger ones.[1]
Other common programs can create temp files too, like sed -i.
Even when a temp file is explicitly requested by a script, there's
no guarantee it will sit around for a long time. Consider sorting
an array. A bash script might do something like:
file=$(mktemp) || die ...
trap 'rm -f "$file"' EXIT
printf '%s\0' "${array[@]}" > "$file"
readarray -d '' array < <(sort -z < "$file")
rm -f "$file"
That temp file won't exist for long.
> > A temp file that's created, written to, read from, and deleted all
> > within such a short span of time will probably *not* be written to a
> > physical device.
>
> I find that assertion highly dubious and dare you to test it.
I lack the knowledge to test it personally by any means other than
watching the disk activity light, which is much harder to do now,
compared to the old days.
I'm also not sure how journals affect this. In the old days, most people
used ext2 for /tmp so it simply wasn't an issue.
[1]
hobbit:~$ strace bash-4.4 -c ': <<< hi'
...
getrandom("\x9a\x8c\x5b\x53\xab\xa3\xd9\xee\x15\xa4\x72\x03\xab\xde\xe7\x9e\x00\xdc\xe5\x61\x6f\x24\xa3\xc8\x48\x1f\x42\xfb\x67\xc5\x28\xb5",
32, 0) = 32
openat(AT_FDCWD, "/tmp/sh-thd.AYa6KQ", O_RDWR|O_CREAT|O_EXCL, 0600) = 3
fcntl(3, F_SETFD, FD_CLOEXEC) = 0
write(3, "hi", 2) = 2
write(3, "\n", 1) = 1
openat(AT_FDCWD, "/tmp/sh-thd.AYa6KQ", O_RDONLY) = 4
close(3) = 0
unlink("/tmp/sh-thd.AYa6KQ") = 0
...
hobbit:~$ strace bash-5.2 -c ': <<< hi'
...
pipe2([3, 4], 0) = 0
fcntl(4, F_GETPIPE_SZ) = 65536
write(4, "hi\n", 3) = 3
close(4) = 0
fcntl(0, F_GETFD) = 0
fcntl(0, F_DUPFD, 10) = 10
fcntl(0, F_GETFD) = 0
fcntl(10, F_SETFD, FD_CLOEXEC) = 0
dup2(3, 0) = 0
close(3) = 0
dup2(10, 0) = 0
fcntl(10, F_GETFD) = 0x1 (flags FD_CLOEXEC)
close(10) = 0
...