0

One of the software tools I'm using requires "sh" to be a symlink to "bash" instead of "dash". Since I don't want to change the default shell to bash for the whole system and I can't change the tool itself, I'm trying to write a script that will alias sh to /bin/bash. Alias itself doesn't work because the call to sh is done by full path: /bin/sh rather than just sh.

Is there a way to fool an executable that /bin/sh is actually another file, without having root privileges and without affecting other programs?

Thanks

2
  • "that will alias ls to /bin/bash" Do you mean "sh to /bin/bash"? If not, that sentence isn't clear to me.
    – dfarrell07
    Aug 8, 2014 at 15:59
  • @dfarrell07 yes, you are correct. Fixed it.
    – rhobincu
    Aug 8, 2014 at 16:23

1 Answer 1

0

If the offending tool is lame enough to call /bin/sh directly, instead of respecting your system setup by using /usr/bin/env sh, your options are very limited. I originally thought you'd be able to basically do what virtual environments do for Python, but after attempting to write a script that did so I couldn't get around the direct call to /bin/sh via any $PATH magic.

  • Before calling the tool, re-symlink sh to bash, then undo your change after the call to the tool.
  • Better yet, change your assumption that you can't edit the tool. What's the tool? If it's open source, which it seems like it is given you have some knowledge of how it works, fork it, change it to use /usr/bin/env sh and submit a pull request. Huzzah for open source contributions!

If the offending tool isn't actually calling /bin/sh directly, as your recent comment may indicate, the following should work.

#!/usr/bin/env sh
BASH_VENV_HOME=$HOME/.bash_venv

if [ ! -d $BASH_VENV_HOME ]; then
    mkdir $BASH_VENV_HOME
fi

# Make an `sh` binary in the front of your path that points to bash
if [ ! -e $BASH_VENV_HOME/sh ]; then
    ln -s /bin/bash $BASH_VENV_HOME/sh
fi
orig_path=$PATH
PATH=$BASH_VENV_HOME:$PATH

# Call your tool

# Reset path
PATH=orig_path

The tool, should it execute sh, will then find the one in the front of your path, which is a symlink to bash.

4
  • It's not open source, it's the Chipscope analyzer from Xilinx. I'm assuming it calls /bin/sh because simply aliasing sh to /bin/bash will allow me to do sh somescript and it will run with bash (even if the symlink in /bin points to dash) but the analyzer still doesn't work. I guess it is possible that it runs env sh, but isn't that the same as calling /bin/sh?
    – rhobincu
    Aug 8, 2014 at 16:30
  • Also, I just noticed that SHELL env variable points to bash!
    – rhobincu
    Aug 8, 2014 at 16:32
  • The example you gave doesn't imply that it directly calls /bin/sh. Aliases aren't evaluated by default in non-interactive mode, so the result you see with sh somescript doesn't relate to the result the script would see.
    – dfarrell07
    Aug 8, 2014 at 16:39
  • And no, env sh and /bin/sh are not the same. The first looks in your path for a binary called sh, the second directly uses the binary /bin/sh.
    – dfarrell07
    Aug 8, 2014 at 16:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .