Quantcast
Channel: sed in-place flag that works both on Mac (BSD) and Linux - Stack Overflow
Viewing all articles
Browse latest Browse all 16

Answer by nwinkler for sed in-place flag that works both on Mac (BSD) and Linux

$
0
0

Here's another version that works on Linux and macOS without using eval and without having to delete backup files. It uses Bash arrays for storing the sed parameters, which is cleaner than using eval:

# Default case for Linux sed, just use "-i"sedi=(-i)case "$(uname)" in  # For macOS, use two parameters  Darwin*) sedi=(-i "")esac# Expand the parameters in the actual call to "sed"sed "${sedi[@]}" -e 's/foo/bar/' target.file

This does not create a backup file, neither a file with appended quotes.


Viewing all articles
Browse latest Browse all 16

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>