Generic shell function for modifying files in-place

inplace() { eval F=\"\$$#\"; "$@" > "$F".new && mv -f "$F".new "$F"; }
Some commands (such as sed and perl) have options to support in-place editing of files, but many commands do not. This shell function enables any command to change files in place. See the sample output for many examples. The function uses plain sh syntax and works with any POSIX shell or derivative, including zsh and bash.
Sample Output
$ inplace nl textfile
(number lines of textfile in-place)
$ inplace awk '{print NF,$0}' somefile
(adds field counter to a file)
$ inplace nroff -man page.1
(convert manpage source to readable "catpage")
$ inplace sort textfile
(sort the lines of a file in-place)
$ inplace sed s/foo/bar/g myfile
(This just serves as an example, sed has an -i option for in-place editing anyway)

By: inof
2010-04-09 11:36:31

What Others Think

Very nice. Of course, you can shorten it by a couple of chars :-) inplace() { eval F=\"\$$#\"; "$@" > "$F".new && mv -f "$F"{.new,}; }
flatcap · 637 weeks and 3 days ago
flatcap: If you shorten it that way, it doesn't work anymore in plain (POSIX) bourne shells (it still works with zsh and bash, though). Personally I prefer it to be as portable as possible, even if it is two characters longer. That way I can easily share my collection of shell functions between different machines (BSD, Linux, Solaris). If you absolutely need to make it shorter, you can omit several of the spaces that I left for better readability. That way you can save more than just two characters, and it's still portable. ;-)
inof · 637 weeks and 3 days ago
FYI I'm working on an inplace script for coreutils that uses some of the techniques mentioned here:
pixelbeat · 637 weeks ago
Ah-ha. Now I see why you downvoted my sedw function. I had not seen this one you wrote. You have some cool stuff; your shell skills are better than mine. But if we're playing keystroke golf, I think sedw 's/old/new/' beats inplace sed 's/old/new/' Beginner's luck. :)
argv · 636 weeks and 1 day ago
argv: I haven't voted on your sedw function (neither up nor down), if I recall correctly. But still, all the major BSDs have -i options for in-place editing, so there's no need in this case. Well, you could make an alias sedw="sed -i" if you want to save two keystrokes. ;-)
inof · 636 weeks and 1 day ago
I just presumed it was you. I will find that guy who voted me down! ;) NetBSD's sed does not have -i. This is why I made the sedi and sedw functions. Because the sed in NetBSD base does not have in-place editing. I used aliases in the early days but now prefer functions. They allow more flexibility and can be used in scripts. I like your approach to in-place editing; it's simple, verstile and portable. I opted to use /tmp because that's what ed and the newer sed's do. They used /tmp for in-place editing. If your sed has an -i option, it's making a tmp file and then removing it. I always have /tmp mounted as mfs or tmpfs. Cheers.
argv · 636 weeks and 1 day ago
argv: Using /tmp is a bad idea, actually, for security reasons. World-writable directories can be used for several kinds of attacks, for example symlink attacks. Even if you use '$$' there is a race condition that can be abused. Some systems provide tools like mktemp(1), but this is not portable. Therefore I try to avoid using /tmp at all, especially on multi-use machines (shell boxes). By the way, FreeBSD's sed doesn't use /tmp either. It creates a backup file in the same directory as the file being edited. I don't know what GNU sed does (I'm too lazy right now to log into a Linux machine for checking), but I guess it's also clever enough to not use /tmp. Many programs recognize the environment variable TMPDIR, so I always set it to a directory that is not world-writable, like TMPDIR=$HOME/tmp (which is 0700, i.e. drwx------).
inof · 636 weeks and 1 day ago
Thanks inof. If one is in a multi-user environment and truly paranoid about symlink-racing, then yes, avoid /tmp. And check all your applications as some of them may use it. It was GNU sed I was thinking of that uses /tmp. I'm too lazy to install it or boot into a Linux to double check. I know ed uses /tmp. And vi uses /var/tmp for vi.recover, correct? I presumed sed's with in-place editing would use /tmp. As usual, BSD does the smart thing. You have given me an idea. The next time I'm questioned why I don't use GNU sed, I can say I'm paranoid about race conditions. Keep up the good work.
argv · 636 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: