2

I'd like to alias sed to gsed, but only while a particular script is running.

I have a script that invokes sed. On the Mac, BSD sed doesn't support the required features, but I don't want to replace it globally with the GNU version.

I can't set an alias in the invoker, as aliases aren't propagated, and I can't set it in a login script as then it will apply globally. The script can't be edited.

I'm posting my current solution, using a nasty symlink/path hack, as an answer, but I'm hoping that someone will come up with a better solution.

2 Answers 2

2
#!/bin/sh
# Runs a command with gsed substituting for sed

set -e

mkdir -p /tmp/temp-path
ln -fs `which gsed` /tmp/temp-path/sed
export PATH=/tmp/temp-path/:$PATH
eval "$@"
2
  • I know this is old, but can you explain it a little? What makes this temporary? And does it leave behind that director? Also I guess 4 years later, have you come up with a better way to do it?
    – Tom Prats
    Mar 15, 2018 at 20:54
  • 1
    It does leave behind the symlink in tmp, but that’s what tmp is for and the OS will clear it out at some point. The key thing is that the substitution only applies to the evaluation of the command because the revised PATH is only in effect to the eval expression Mar 17, 2018 at 8:03
0

One alternative is to install the GNU versions of utilities in a specific directory, such as /usr/local or /usr/local/gnu

Then when you wish to use that specific app in a script you would refer to it explicitly as /usr/local/gnu/bin/sed

Another option would be to assign a SED variable while is exportable.

SED=`which gsed`
export SED

Thereafter call sed via ${SED}

2
  • Ah, I should have mentioned, the script can't be edited! Feb 11, 2014 at 21:24
  • Update your question to include that information, as well as including your current solution which you've included as an answer.
    – bsd
    Feb 11, 2014 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.