Bash file skeleton to use as a launcher

Elvis Ciotti
1 min readJan 25, 2024

--

If what you need is just a script that launched various commands by name passed to the CLI, and a command can launch another command, then you can create a bash file with each command in a function, then launching eval with each argument, that will launch the function defined with that name.

You can also define a target called help that prints all the defined function by using declare -F | awk ‘{print $3}’

Complete example

#!/bin/bash

export COMMON_VAR1=...

test() {
npm run test || exit 1
}

help() {
declare -F | awk '{print $3}'
}

build() {
npm ci
npx tsc
npm install --production
}

if [[ "$1" = "" ]]; then help; fi

for arg in "$@"; do
eval "${arg}"
done

Rename this file to runand run chmod +x run just once.

You can now launch commands like this

# print functions list
./run

# tests
./run test

# run test and then build
./run test build

--

--

Elvis Ciotti
Elvis Ciotti

Written by Elvis Ciotti

Software Contractor — Java, Spring, k8s, AWS, Javascript @ London - hire me at https://elvisciotti.github.io/

No responses yet