Jonathan Samson
Jonathan Samson

Reputation: 83

How do I use to the zsh 'command' option to execute a the builtin 'source' command?

I am trying to log each time my shell sources a file. I am using zsh, so I went into zshenv and added this function.

source() {
    echo "sourcing $1"
    command source $1
}

The idea is everytime "source [file]" appears in one of my dotfiles and is executed, it should print the action to terminal first, before actually sourcing the file.

instead i'm getting some errors like this

sourcing /Users/js/.cargo/env
source:2: command not found: source
sourcing /Users/js/.sources/postgres-env.sh
source:2: command not found: source
sourcing /Users/js/.oh-my-zsh/oh-my-zsh.sh
source:2: command not found: source
sourcing /Users/js/.iterm2_shell_integration.zsh
source:2: command not found: source

What is the correct way to use the shell 'command' option with zsh to call source here?

Upvotes: 1

Views: 476

Answers (2)

glenn jackman
glenn jackman

Reputation: 246744

command is intended to specifically invoke external commands. For example if you have an alias or function for git, command git will bypass those.

You're looking for the builtin command to limit command lookup to only builtin commands.

source() {
    echo "sourcing $1"
    builtin source "$1"
}

Upvotes: 6

Léa Gris
Léa Gris

Reputation: 19545

For it to work regardless of shell, you could use this instead:

#!/usr/bin/env sh

source() {
  echo "sourcing $1"
  . "$1"
}

source "$1"

Upvotes: 0

Related Questions