Here's a less intrusive alternative by way of a simple alias:
alias lx='PATH="./bin:$PATH"'
You can then use lx
(Local eXecution) in any folder that has a bin
subfolder to execute scripts there without a path; e.g., to execute script ./bin/start-server
from the current directory, run:
lx start-server
If you want to enable tab-completion based on the local executables, add the following to your bash profile (tested on OSX and Linux):
# Install the custom tab-completion function defined below.
complete -F _complete_lx -- lx
# Command-completion function for lx: tab-completes the executables in
# the ./bin subfolder.
_complete_lx() {
# Set the directory in which to look for local executables.
local localExeDir='./bin'
# Get the current command-line token.
local token=${COMP_WORDS[$COMP_CWORD]}
local tokenLen=${#token}
# Find all local executables.
read -d ' ' -ra localExes < <(find "$localExeDir" -maxdepth 1 -type f -perm -a=x -exec basename -a {} +;)
# Filter the list of local executables
# based on the current command-line token.
# Turn case-insensitive matching temporarily on, if necessary.
local nocasematchWasOff=0
shopt nocasematch >/dev/null || nocasematchWasOff=1
(( nocasematchWasOff )) && shopt -s nocasematch
COMPREPLY=()
for localExe in "${localExes[@]}"; do
if [[ ${localExe:0:$(( tokenLen ))} == "$token" ]]; then
COMPREPLY+=( "$localExe" )
fi
done
# Restore state of 'nocasematch' option, if necessary.
(( nocasematchWasOff )) && shopt -u nocasematch
}