auto_execok

auto_execok, a command in the Tcl library, determines whether an executable file, or shell builtin, exists, based on its single argument. See auto_reset for info on how to clean up the cache of auto_execok.


On Unixy platforms, scripts may get away with exec [auto_execok $progname] $firstarg ..., but this is in general wrong, since auto_execok always returns a list of arguments for exec!

Canonical example usage:

eval exec [auto_execok myCommand] [list $arg1 {Some text} $arg3]

or, with 8.5:

exec {*}[auto_execok myCommand] $arg1 {Some text} $arg3

[Explain why this is better than $::env(COMSPEC), cmd.exe, ...]

LES 2004-09-16: auto_execok is better than $::env(COMSPEC) and cmd.exe? Get outta here... :-)


To demonstrate why eval was required before 8.5:

% auto_execok start
C:/WINNT/system32/CMD.EXE /c start
% exec [auto_execok start] https://wiki.tcl-lang.org
couldn't execute "C:\WINNT\system32\CMD.EXE \c start": invalid argument
% eval exec [auto_execok start] https://wiki.tcl-lang.org
% # success

Note that if any arguments to "start" have spaces (or other white space, or other syntactically-privileged characters) in them, you have to list-protect them from eval:

% set mydoc "My document.doc"
% eval exec [auto_execok start] $mydoc
% # msword may complain about being unable to find "My" and "document.doc"'
% eval exec [auto_execok start] \"\" [list $mydoc]; # should be more successful
% # Dont't forget the '\"\"' otherwise "My document.doc" is used as the window-titel for
% # a DOS-shell.

See Also

exec
exec ampersand problem
invoking browsers
clear screen
COMSPEC