skip to Main Content

So I have this basic terminal app, which the contents of which I don’t think matter. I would like to be able to run the application with a custom command, simply something like: foo.
I have had times where when I download a ZIP file and open it, all of a sudden there is a new terminal command at my disposal that runs the application without using ./ at all.
TL;DR: What code should I put in a Makefile to create a terminal command?
I am using Debian Linux, but a cross-platform solution or a few different solutions would all be appreciated.

I have tried simply writing code into the file: .bachrc, but that would not be an easy thing for an average user to do. I have also tried concatenating some text to it, but that never seemed to work for me.

2

Answers


  1. Create a bin directory in your home directory

    $ mkdir ~/bin
    

    Move your terminal app to the newly created bin directory

    $ mv /path/of/foo ~/bin/foo
    

    Then add your local bin to your PATH environment variable by adding this to the end of your .bashrc:

    # set PATH so it includes user's private bin if it exists
    if [ -d "$HOME/bin" ] ; then
        PATH="$HOME/bin:$PATH"
    fi
    

    In a new terminal window try to find your command.

    $ which foo
    

    You should get output that looks something like this:

    /home/username/bin/foo
    

    NOTE: if you prefer you can create a symbolic link to your terminal app instead of moving it, like this:

    $ ln -s /path/of/foo ~/bin/foo
    
    Login or Signup to reply.
  2. The usual mechanism is to have an install: target in your Makefile which installs the script or binary into /usr/local/bin; but you also want to be able to allow the user to override this (so that, for example, a Debian package can use your Makefile but install to DEBIAN/usr/bin instead).

    prefix=/usr/local
    binpath=$(prefix)/bin
    install: scriptfile
        install -m 755 scriptfile $(DESTDIR)$(binpath)
    

    Here, scriptfile is a script or binary executable which you want to install in /usr/local/bin/scriptfile which should then make it available to any user with a sane PATH.

    For good measure, probably also include uninstall;

    uninstall:
        $(RM) -f $(DESTDIR)$(binpath)/scriptfile
    

    If someone wants to install to their home directory, they can make prefix=$HOME or some such.

    DESTDIR is a common convention for setting the installation root; so, for example, a Debian packager would set DESTDIR=DEBIAN/ and prefix=/usr. (A more complex project might also install libraries in $(DESTDIR)$(prefix)/lib, man pages in $(DESTDIR)$(prefix)/share/man, etc.)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search