Create Executables, not Shell Aliases or Functions

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.

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

This blog post is 100% human-written, and has nothing derived from LLMs (be it code, editorial support or prose).

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.