smead
smead

Reputation: 175

Cannot source script in .cshrc

A library I'm using has a script to set the appropriate environment variables:

# Source this script to set up the ROOT build that this script is part of.
#
# Conveniently an alias like this can be defined in ~/.cshrc:
#   alias thisroot "source bin/thisroot.sh"
#
# This script if for the csh like shells, see thisroot.sh for bash like shells.
#
# Author: Fons Rademakers, 18/8/2006

# $_ should be source .../thisroot.csh
set ARGS=($_)
set THIS="`dirname ${ARGS[2]}`"
setenv ROOTSYS "`(cd ${THIS}/..;pwd)`"

set path = ($ROOTSYS/bin $path)

if ($?LD_LIBRARY_PATH) then
   setenv LD_LIBRARY_PATH $ROOTSYS/lib:$LD_LIBRARY_PATH      # Linux, ELF HP-UX
else
   setenv LD_LIBRARY_PATH $ROOTSYS/lib
endif

if ($?DYLD_LIBRARY_PATH) then
   setenv DYLD_LIBRARY_PATH $ROOTSYS/lib:$DYLD_LIBRARY_PATH  # Mac OS X
else
   setenv DYLD_LIBRARY_PATH $ROOTSYS/lib
endif

if ($?SHLIB_PATH) then
   setenv SHLIB_PATH $ROOTSYS/lib:$SHLIB_PATH                # legacy HP-UX
else
   setenv SHLIB_PATH $ROOTSYS/lib
endif

if ($?LIBPATH) then
   setenv LIBPATH $ROOTSYS/lib:$LIBPATH                      # AIX
else
   setenv LIBPATH $ROOTSYS/lib
endif

if ($?PYTHONPATH) then
   setenv PYTHONPATH $ROOTSYS/lib:$PYTHONPATH
else
   setenv PYTHONPATH $ROOTSYS/lib
endif

if ($?MANPATH) then
   setenv MANPATH `dirname $ROOTSYS/man/man1`:$MANPATH
else
   setenv MANPATH `dirname $ROOTSYS/man/man1`
endif

The script works fine if I run it in the command line, e.g.

source /home/sm/packages/root/bin/thisroot.csh

However if I try to add that command to my .cshrc file, it fails with the error message:

ARGS: Subscript out of range.

What gives?

Upvotes: 0

Views: 2688

Answers (1)

Keith Thompson
Keith Thompson

Reputation: 263557

This line appears to be the problem:

set ARGS=($_)

According to the tcsh(1) man page:

$_ Substitutes the command line of the last command executed. (+)

The "(+)" indicates that this is a tcsh-specific feature (though /bin/bsd-csh on my Linux system appears to support it as well).

A quick experiment indicates that if I type

source foo.csh

from an interactive tcsh prompt, it sets $_ to the string source foo.csh -- but if I execute the same command from a csh or tcsh script, it doesn't. (If the script is run using csh, $_ is set to /usr/bin/tcsh; if the script is run using tcsh, $_ is set to the empty string.)

As far as I can tell this inconsistent behavior is not documented anywhere -- and the csh(1) man page doesn't mention $_ at all.

(This kind of inconsistency is common when using csh/tcsh. You'll find that the Bourne shell sh, and shells derived from it such as ksh, bash, and zsh, behave much more consistently. Obligatory link: Csh Programming Considered Harmful).

Upvotes: 2

Related Questions