How to execute a script with "source" from .bashrc?

19 Views Asked by At

I have this script:

$WORKSPACE/bt-helper/navigate_to_project.sh

#!/bin/bash

this_dir="${0%/*}"

echo -e "Script dir: $this_dir"
echo -e "Argument 0: $0"
echo -e "Argument 1: $1"

source "$this_dir"/commons.sh

project="$1"
validate_project_name "$project" || return 1

project_name="${project//-/_}" # Replace dashes with underscores
project_upper=$(echo "$project_name" | awk '{print toupper($0)}')
path_var="BT_${project_upper}_PATH"
path="${!path_var}"

if [[ -z "$path" || ! -d "$path" ]]; then
    echo -e
    print_error "Path for ${PROJECT_COLOR}$project${NC} not found or invalid."
    echo -e
    return 1
fi

print_info "Navigating to: ${PROJECT_COLOR}$project${NC}"

cd "$path" || return 1

print_ok "Current location: ${PROJECT_COLOR}$project${NC}"

echo -e

and it works fine here:

$WORKSPACE/bt-helper/mvn_clean_install.sh

#!/bin/bash

this_dir="${0%/*}"

source "$this_dir"/commons.sh

mvn_clean_install() {
    local project="$1"
    validate_project_name "$project" || return 1

    echo -e
    print_info "Performing clean install (with tests) on: ${PROJECT_COLOR}$project${NC}"

    . "$this_dir"/navigate_to_project.sh "$project" && mvn clean install

    echo -e
    print_ok "Clean install (with tests) complete for: ${PROJECT_COLOR}$project${NC}"
}

if [[ "$1" == "all" ]]; then
    for project in "${ALL_PROJECTS[@]}"; do
        mvn_clean_install "$project"
    done
    return 0
fi

mvn_clean_install "$@"

But it doesn't work in my .bashrc

This is the output of the debugging echo I added in the script:

$ cd-bt some-project
Script dir: /usr/bin
Argument 0: /usr/bin/bash
Argument 1: some-project
bash: /usr/bin/commons.sh: No such file or directory
bash: validate_project_name: command not found

I expected argument 0 to be the true value of $WORKSPACE/bt-helper. Can someone help me understand what is going on? I use Git Bash for Windows.

Neither of those cases work, I combined them all in one code block, but I wrote them separately, this does not represent my .bashrc

~/.bashrc

cd-bt() {
    . "$WORKSPACE"/bt-helper/navigate_to_project.sh "$@"
}

alias cd-bt='. $WORKSPACE/bt-helper/navigate_to_project.sh'
0

There are 0 best solutions below