1

I have a compiled program in a certain folder that I would like to access from anywhere without needing the write ./path/to/file/each/time/app_name. For example, the app executable is installed in this ./path/to/file/each/time/app_name path, and I would like to be able to open it by writing app_name in the command line, and nothing more than that. How could I do that?

2 Answers2

1

Create a script (as root) called /usr/local/bin/app_name and put this inside it:

#!/bin/bash

/path/to/file/app_name $@

Then make the script executable:

sudo chmod +x /usr/local/bin/app_name
  • Better also explain why and how that works. You may also want to complete the answer by adding another option, i.e. to place a symlink to the executable in /usr/local/bin. In addition, it is worthwhile mentioning the option to use ~/bin or ~/.local/bin if only the current user needs access - in that case, no root access is needed. – vanadium Sep 10 '21 at 11:19
  • ~/.bin and such are not in $PATH by default so that's a deeper rabbit hole. There are other problems with symlinks. This solution works everywhere regardless of those problems and also allows you to do more flexible things like add default arguments or run more complex composite commands. – Kristopher Ives Sep 11 '21 at 12:35
  • .bin is automatically included in the path if it exist on Ubuntu, so no rabitt hole there. There is more rabitt holes in trying to do everything as root. With symlinks, there will be no more problems than with your approach. If an app requires you to start in a specific folder or set an environment before, then indeed a wrapper script is the way to go. – vanadium Sep 13 '21 at 10:10
  • Nope. The default path for 20.04 LTS is /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin – Kristopher Ives Sep 14 '21 at 12:24
  • You must have a different linux version then, or you are using a different shell. – vanadium Sep 14 '21 at 13:09
  • Nope. These are default $PATH values, see https://askubuntu.com/questions/386629/what-are-the-default-path-values – Kristopher Ives Sep 14 '21 at 14:10
  • ~/bin is added to PATH if and only if it exists (as vanadium says). You can find the code that adds it in ~/.profile – Zanna Sep 14 '21 at 14:50
0

Create an alias for your program in the .bashrc file.

nano ~/.bashrc

At the end of the file type the line:

alias app_name=/path/to/file/each/time/app_name

Then save (CTRL+O then Enter) and exit (CTRL+X).

You will be able to use the alias in new Terminal windows you open.

Alejandro
  • 695