Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • In a way, it's odd that sponge itself doesn't provide the wrapper functionality... But it's also impossible to make a really generic wrapper, because an arbitrary tool might edit the given file in-place itself, and not print to stdout. Using the wrapper with one like that might be problematic. Also there's the question of caching the data to memory vs. to a temporary file, and overwriting the target in-place vs. creating a new file with the same name... (not sure what sponge does) Commented Oct 10, 2023 at 20:54
  • 1
    Anyway, quote the expansions so that your function works with arbitrary filenames: inplace() { "${@:2}" < "$1" | sponge "$1" } Commented Oct 10, 2023 at 20:56
  • Related, first solution in this answer Commented Oct 10, 2023 at 23:14
  • @ilkkachu, rather inplace() { "${@:2}" < "$1" | sponge -- "$1"; } to deal with arbitrary file paths. Or inplace() (file="${1?}"; shift; <"$file" "$@" | sponge -- "$file") to avoid that ksh93ism. Setting the pipefail option in there would also be useful to report failure of the command (in any case, the file is lost if the command fails). Commented Oct 11, 2023 at 5:47
  • @StéphaneChazelas, yes, indeed Commented Oct 11, 2023 at 5:52