In a script I'm debugging, I see it use $_ at the beginning of the file. It appears to suck in the full command that called the file. When trying to wrap this script in another script though, the $_ doesn't appear to suck in anything at all which causes the rest of the script to bomb.

I tried using $_ later in the script and it appears to take the previously executed command and either reruns it or just saves the output of the last command.

So if it is saving the results of the last saved command, why is it empty when another script calls into the script trying to use $_ at the beginning?

Maybe the real question is what is different about the environment between a script and the cmd line that would result in this different behavior, and what would be the WA or solution since the subscript I'm debugging isn't writable by me.

The below code also shows some weirdness in that if echo $_ is in the first line of a script, it pulls the previous cmd run on the cmd line, but if echo $_ is used again, it always outputs the cmd used to call the script.


echo $_
echo $_
echo $_

cmd line:

>echo "foo"
>echo "bar"
>source ./test
echo "bar"
source ./test
source ./test

1 Answer 1


man tcsh:

   $_      Substitutes the command line of the last command executed.  (+)
  • Any idea what the "last command executed" is defined as? Why would the first call of $_ in a script differ from any others? I'm either misunderstanding the definitions or this just looks buggy to me.
    – horta
    Feb 5, 2015 at 19:53
  • @horta A practical example of how to use $_, for creating a folder (including subfolders) and then changing into it: mkdir -p /home/foo/bar && cd $_ Jun 18, 2018 at 18:22

You must log in to answer this question.

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