2

I know that I can add a path to the $PATH on the bash terminal with the command PATH=$PATH:foo . I also know I can create an alias for the bash with the command alias a=b But only know I noticed that an alias for the bash is not a global alias and some tools might be unable to find (which is very reasonable ,if the documentation says that the tools just search the path for another dependency).

So how can I add something to the path with a different name so that every other command line tool will find it easily ? I can think of at least three uses for my question [g++9 as default (this can be solved with update alternatives), python 3 as default and not 2] but my main question occurred when I tried to install bazelisk as bazel because tensorflow build by source configurations need bazel , for your info I installed bazel with go in the default directory , which is ~/go/bin/bazelisk

3

You could do that in two steps:

  1. Make sure that your path starts with a directory in which you want to provide the "alias" names. For example, we could use the bin/ directory in our $HOME directory:
export PATH=$HOME/bin:$PATH
  1. Put the alias names into that directory as symbolic links. For example, to create an alias name python to python3, run
ln -s $(which python3) $HOME/bin/python

Notes:

  • You do not need to be root to do this.
  • Be careful when you do this. Doing "weird" things like ln -s /bin/cat $HOME/bin/ls will backfire as much as talking to your neighbor and calling a car a fish.
  • Your "alias" directory doesn't need a special name. We're not inventing a new concept here, we're just directly using how $PATH works.
  • It will not work if your "alias" directory is last in $PATH, it must be first.
  • Do not replace which with command -v. command -v will misbehave: It will print aliases, print shell-builtin commands without path, and in such situations, where a symbolic link could not be created, it would succeed giving wrong results, whereas which would reliably return an empty string plus set the exit status to 1, allowing a shell script to behave properly using if alias=$(which python3) ; then ln -s $alias ~/bin/python ; fi. Or alternatively just fail by using set -e; alias=$(which python3); ln -s $alias ~/bin/python. Using command -v will give wrong results in case of aliases and shell-builtin commands.
| improve this answer | |
  • The suggested edit to replace which with command -v is harmful in shell scripts. In shell scripts, errexit should be set. In case of an alias, command -v would succeed, printing alias l='ls -CF' for example, and the ln would do garbage. In case of an alias, which fails, stopping the script if errexit is set. – Christian Hujer Jun 23 at 18:44
  • Another reason can be seen by command -v echoecho vs which echo/usr/bin/echo. In case of shell built-in commands, command will give the wrong result that will make the symbolic link fail. – Christian Hujer Jun 23 at 19:04

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.