a bash script can be BASEDIR=$(dirname $0) but how do I do that in expect script ?

If I use BASEDIR=[dirname $argv0] I get the error "invalid command name "dirname".


Various file operations in TCL are organized under the file(n) procedure which has a dirname command.

% expect
expect1.1> file dirname $argv0

Note however this may not be set, valid, nor checked for validity depending on exactly how the TCL (nor the shell code, for that matter) is run, so you may want error checking; the directory will also vary depending on how the code is run.

% cat dirname
#!/usr/bin/env expect
set basedir [file dirname $argv0]
puts $basedir
% chmod +x dirname
% ./dirname
% ~/tmp/dirname

Use file normalize if you always want a fully qualified path.

% expect
expect1.1> set basedir [file normalize [file dirname $argv0]]

There is alo the Tcl_FindExecutable(3) call, which exposes the executable name (if available) via the info nameofexecutable command:

% expect
expect1.1> info nameofexecutable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.