Every weekend I tinker with my new open-source project called bb8, a FastAPI CLI.

https://github.com/thebigdatajedi/bb8

I either add TODOs to the list to ensure I know what I want the CLI to do next. I add a little tiny bit of functionality here and there.

I started the project a few weeks back to challenge myself that I could create an app with a tiny feature surface that was functional in a programming language I knew nothing about. The project would be my entry into learning the language, and bb8 was born. The language I challenged myself to learn was Go. Knowing nothing about it, I embarked on a fantastic fun journey.

In moving my deployment scripts from the root to a scripts directory, I could no longer access the recently built binary that builds to the root dir to do an mv <source_binary> <target_dir> using the relative path notation .. after a bit of research I discovered that bash would not make any automatic assumption about where the running script was located so I had to give it context as to its working directory.

So::

mv ../<source_binary> <target_dir>

was not working to save my life.

Bash kept reporting there that was no such file or directory and it didn’t even tell me which of the two parameters to mv it wash choking on.

Establishing working dir context

Here is the code that does the move.

#if the target directory exists
echo "$(date) - checking if target directory exists && source binary exists"
if [ -d "$target_dir" ]; then
  #and if the source binary exists
    if [ -f "$source_binary" ]; then
        #then move the source binary to the target directory
        **mv "$source_binary" "$target_dir"**
    #else message user
    else
        echo "$(date) - $source_binary does not exist"
        exit 1
    fi
#else message user
else
    echo "$(date) - $target_dir does not exist"
    exit 1
fi

Here is the caller::

echo "$(date) - deploy started"
**./macOS_deploy.sh "$working_dir/../$source_binary" $target_directory $source_binary**
echo "$(date) - deploy complete"
# end of script

Here is the bash working dir context building code with the caller::

echo "$(date) - bootstrapping working dir started"
**working_dir=$( cd "$(dirname "${BASH_SOURCE[0]}")" || exit ; pwd -P )**
cd "$working_dir" || exit
echo "$(date) - bootstrapping working dir complete"

echo "$(date) - deploy started"
./macOS_deploy.sh "$working_dir/../$source_binary" $target_directory $source_binary
echo "$(date) - deploy complete"
# end of script

Here is where the magic happens::

**working_dir=$( cd "$(dirname "${BASH_SOURCE[0]}")" || exit ; pwd -P )
cd "$working_dir" || exit**

What I mean by the magic is, the calling code is calling macOS_deploy.sh and it’s doing it in an ever changing world. Hence we’ve been trained to use relative paths. But bash is not aware where the current script or the caller is located so, I have to get that and this is a dynamic value based on where the script is located at any time in the system which I thought the need for this code was peculiar and reset my understand of what bash knows and doesn’t know automatically but what I found hallarious was that the system needs an explicit cd to that directory.

Basically the code above says, get where I’m located and then cd there.