mles mles - 6 months ago 39
Bash Question

Prevent sed from creating a temporary file

I'm injecting a proxy variable into a shell script. I have to escape the string as there is a space in between.

sed -i -e 's#UseConcMarkSweepGC#UseConcMarkSweepGC $PROXY_ARGS#g' ${TEST_HOME}/bin/test.sh


Now this is always generating another file ith
-e
at the end, so I end up with
test.sh
and
test.sh-e
. Both are the same. Currently I'm doing

rm test.sh-e


right after
sed
which is working fine. I'm wondering if there is a way to not generate a temporary file at all? I can't remove the
-e
as I'm getting the error
sed: -i may not be used with stdin
.

Answer

This is because of the order you are placing the flags, if you are under a non-GNU sed that requires -i to have an argument:

sed -i -e '...'
#   ^^ ^^
#   |   |
#   flag|
#       |
#       value for it

Like this, sed understands -e as the extension of the backup file to create.

Instead, say:

sed -e -i '...'

Or just be explicit about the extension and set it to null, so that no backup file is created:

sed -i '' -e '...'

See more information in sed: -i may not be used with stdin on Mac OS X.