Skip to content
Advertisement

Executable Unix script cannot be found by which command

I have a Unix bash script written by a former teammate that must be in my PATH, though I can’t find it by manual inspection (see below). I can however execute it anywhere by just typing

$ my_script

I would like to view and edit this script. However, when I try to find it via the which command, I get a blank response. Return code indicates an error:

$ which my_script
$ echo $?
1

And yet, I can run the script. I manually combed my PATH and could not find it. Honestly, I have not encountered anything like this in 20+ years. Is there any other command besides which, and/or ways such a script could be hidden?

Advertisement

Answer

To determine the type of any command callable from the shell, use the type builtin.

In your case, since the presumptive script turned to be a shell function, you would have seen (assuming bash):

$ type my_script  # Bash: option -t would output just 'function`
my_script is a function
my_script () 
{                
...               # The function's definition

From there – as you did – you can examine the profile (e.g., ~/.bash_profile) and / or initialization scripts (e.g., ~/.bashrc) to determine where the function was sourced.

Caveat: The function signature output by type is normalized to the <name> () form – even if you defined the function as function <name>

In bash you can even find out directly where a given function was defined (tip of the hat to this superuser.com answer and Charles Duffy for inspiring me to find it and coming up with the most succinct form):

$ (shopt -s extdebug; declare -F my_func)
my_func 112 /Users/jdoe/.bashrc   # sample output

112 is the line number inside the script indicated.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement