1

I can't count the number of times I've typed: source en<tab> only to be left with a bunch of garbled text on the screen because it sourced the program env instead of the local env.sh.

I tried making a simple function to detect this particular use case but it didn't work.

This is what I tried:

source () {
    if [ "$1" == "env" ]
    then
        source ./env.sh
    else
        source $@
    fi
}

I realize that source is a shell command which is probably why it didn't work but I don't really care about how its implemented, I just want to stop sourcing binaries on my $PATH before the local directory.

Cheers!

Caustic
  • 475
  • 3
  • 14

2 Answers2

1

The reason it doesn't work is that you're calling your function recursively. Use builtin source to call the builtin source rather than your function source:

source () {
    if [ "$1" == "env" ]
    then
        builtin source ./env.sh
    else
        builtin source $@
    fi
}
that other guy
  • 116,971
  • 11
  • 170
  • 194
0

Rather than override a general command (source) to handle a special case, make a new command to handle the special case.

locenv () {
    source ./env.sh
}
chepner
  • 497,756
  • 71
  • 530
  • 681