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 sdaau for sed in-place flag that works both on Mac (BSD) and Linux

$
0
0

If you need to do sed in-place in a bash script, and you do NOT want the in-place to result with .bkp files, and you have a way to detect the os (say, using ostype.sh), -- then the following hack with the bash shell built-in eval should work:

OSTYPE="$(bash ostype.sh)"cat > myfile.txt <<"EOF"11112222EOFif [ "$OSTYPE" == "osx" ]; then  ISED='-i ""'else # $OSTYPE == linux64  ISED='-i""'fieval sed $ISED 's/2222/bbbb/g' myfile.txtls # GNU and OSX: still only myfile.txt therecat myfile.txt# GNU and OSX: both print:# 1111# bbbb# NOTE: # if you just use `sed $ISED 's/2222/bbbb/g' myfile.txt` without `eval`,# then you will get a backup file with quotations in the file name, # - that is, `myfile.txt""`

Viewing all articles
Browse latest Browse all 16

Trending Articles





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