I am trying to store a list of specific files into a variable in expect.

In my script, I am trying to compile files that have different file extensions, like .foo, .bar.

In this example, I have the following files:

  • something.foo
  • something_new.foo
  • something_different.bar
  • this_is_the_wrong_one.sh

To get just .foo files, I tried to run this:

set files "[exec ls -h | grep -E '\.foo' | rev | cut -c5- | rev]"
send_user "$files\n"

I was expecting to see:

  • something
  • something_new

However, I ended up getting this error message:

child process exited abnormally
    while executing
"exec ls -h | grep -E '\.foo' | rev | cut -c5- | rev"
    invoked from within
"set files "[exec ls -h | grep -E '\.foo' | rev | cut -c5- | rev]""
    (file "./runthis.sh" line 2)

I know I could use find '*.foo', ls -h *.foo, awk, etc. I noticed the pattern... with find, expect wigs out if I use the ' (single quote), so that pretty much eliminates the idea of using find, awk, and grep. If I use ls -h *.foo and the files don't exist, the script will stop. It is supposed to continue checking and listing other files.

If it finds the files, it will eventually be split into an array using this command:

Update 1:

set split_files "[split $files \n]"

1 Answer 1


The problem is that single quotes are not special to tcl, so you are actually running the grep command with the argument '\.foo', including the quotes which normally you would expect to be removed by the shell.

This makes the grep fail to match, so it exits with a non-zero error code, and so the exec command passes on the error.

The immediate answer is to use tcl double-quotes (grep -E "\\.foo") or no quotes (grep -E \\.foo) or brace-quotes (grep -E {\.foo}). But an alternative is to not resort to shell commands. The equivalent in tcl might be:

set fullfiles [glob *.foo]
foreach f $fullfiles { lappend files [string trimright $f .foo] }
  • I tried using glob, but it seems the linux server doesn't support glob. Sadly, I don't have the authorization to install it on the server. I did further testing with ls and grep and it turns out that expect probably pukes on the grep part. Hrmm... Sep 6, 2018 at 21:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .