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""`