At first I created a script to 'find' a file and switch to that directory. Alas, upon returning from the script, the 'cd' was unchanged. Directory changes within a script are local to that script. I forgot. Sue me.
So... I created that same code as a function in the middle of .bashrc. When I re-enter the Bash shell, the function is not defined or visible. So... I placed the function at the end of .bashrc and -- voila! -- it worked. Here is the function:
function goto {
if [[ "$1" == "" ]]
then
echo "[ERROR] $0 requires a filename as input."
echo "[INFO] Usage: $0 <filename> finds file and changes to that directory."
else
echo "[INFO] Looking for file: $1"
declare -x -a full_filepath=$(find . -name "$1")
if [[ "${full_filepath[0]}" == "" ]]
then
echo "[ERROR] Unable to find requested file $1. Exiting..."
else
local filepath=${full_filepath[0]%/*}
local filename=${full_filepath[0]##*/}
echo "[INFO] Switching to $filepath to locate $filename..."
cd $filepath
fi
fi
}
Now here's the problem. I had to move it after SDKMan's init code in .bashrc (ignoring the warning that #THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!). Not surprisingly, 'sdk' no longer works.
Is there a "right way" to include a function in .bashrc so that other scripts like SDKMan's can remain at the end, for whatever-in-gods-name reason it must be there...???