Create Executables, not Shell Aliases or Functions
This post's featured URL for sharing metadata is https://www.jvt.me/img/profile.jpg.
I am a fan of making it my life easier and more efficient by using time-saving tools like command-line tooling to save the number of keystrokes I have to enter for common interactions..
For instance, as I use Gradle a lot, so find myself writing the following a lot:
./gradlew clean build
./gradlew spotlessApply
./gradlew spAp # the shorter alternative I actually use
./gradlew bootRun
I ended up writing a shell alias to simplify this, which allows me to instead invoke Gradle with g
:
alias g=./gradlew
However, the crux of this post is that using an alias
/ function
in your shell's language don't work outside of your shell.
For instance, if you want to try and make sure that all commits on a branch pass their tests, you may write:
$ git rebase origin/HEAD --exec 'g clean test'
But unfortunately, that will land you with the following error:
Executing: g
error: cannot run g: No such file or directory
warning: execution failed: g
You can fix the problem, and then run
git rebase --continue
This is because Git is executing commands, without using your current terminal environment, meaning your customisations like alias
/ function
won't be available.
The solution instead is to make sure that you create an executable file, for instance:
mkdir -p ~/bin
export PATH=$PATH:$HOME/bin
touch ~/bin/g
chmod +x ~/bin/g
Where the ~/bin/g
script has the contents:
#!/usr/bin/env bash
./gradlew "$@"
I've currently got 17 helpers, for copy/pasting from the command-line, pretty-printing JWTs, JSON, etc, and it makes life a lot easier.
There are some aliases in my config that aren't yet migrated over - because I don't need them / use them enough to be useful as an executable script.