Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Expect use environment variable if defined

Here is how to use a variable

set a FOOBAR
puts "the contents of a is $a"

Here is how to use an environment variable in expect/tcl.

puts "env var MYVAR is $env(MYVAR)"

Here is what happens when you try to use an environment variable if it is undefined.

$ ./mine.exp
no such variable
    (read trace on "env(MYVAR)")
    invoked from within
"puts "$env(MYVAR)""
    (file "./mine.sh" line 2)

Here is how to use a fallback value if the environment variable fails.

set MYVAR defaultvalue
catch {set MYVAR $env(PREFERRED_VALUE)}
puts "$MYVAR"

References

Weblinks

  1. How to access environment variables in an Expect script? - Stack Overflow

man

man(1)

Comments