Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile to add shell aliases

Tags:

bash

sh

makefile

I am already using a makefile, and I was hoping to be able to use it to store a few useful aliases that the user could then invoke. I know that I can make a bash file with the aliases already built in, so and run it with source, so I can do something like:

# File: aliases.sh
alias useful="command to run"
alias also-useful="another command -to run"

Then I can run this in the current terminal session with:

source ./aliases.sh

Using a Makefile

So I was hoping to achieve something similar with a makefile, I was hoping to have a simple aliases entry, so the user could just run:

make aliases

I prefer to avoid adding an extra file if this is at all possible, because I don't want to add extra files for such simple tasks. If you have any suggestions that would be better, I'd be open to hearing them too.

like image 453
user2662833 Avatar asked Sep 12 '25 23:09

user2662833


1 Answers

If what you're asking is for make aliases to create aliases that you can then invoke at your shell prompt, something like:

$ make aliases
$ useful

then that is impossible and the reason has nothing to do with make.

In a UNIX/POSIX system the process hierarchy is strict: a process starts one or more sub-processes, and each of those can start more, etc. So a login manager process starts your shell (or your window manager), your shell starts make, which is another process, and make will run another shell as a subprocess to run each recipe, and each shell will run programs like compilers, commands like rm which are also processes, etc.

It is a fundamental rule of all processes that they cannot modify the environment (memory) of their parents (and they can only modify the environment of their children before they are started). So, if you start a new shell and change your working directory then exit that shell, the parent's shell is not changed. If you set an environment variable in the child process, the variable is not set in the parent. Etc.

Shell aliases are part of a particular shell's memory. So a program you start cannot create aliases in its parent shell. It doesn't matter if that program is make or anything else.

That's why you have to use the special command source to load those into your shell: instead of running a new shell, the source command tells the current shell to run the commands in the script as if you'd typed them in at the command line... so no new process is created and the current shell's environment and memory is modified. If you ran your aliases file as a shell script, via aliases rather than source aliases, then a new shell would be created, the aliases would be defined, then the shell would exit and all the aliases would be gone again.

So, all that to say it's not possible for make to define aliases in the shell that invokes it: the operating system won't allow it.

like image 72
MadScientist Avatar answered Sep 14 '25 15:09

MadScientist