C shell script passing arguments problem.

I found something insteresting when I tested passing arguments into my scripts.
My scripts is as below.

% cat passarg.env 
#!/bin/csh 

echo "passarg: argv[1] = $argv[1] argv[2] = $argv[2]"
passarg1.env $*
% cat passarg1.env
#!/bin/csh

echo "passarg1: argv[1] = $argv[1] argvp[2]=$argv[2]"
set str = "test passing arguments"
source passarg2.env "$str"
% cat passarg2.env
#!/bin/csh

echo "passarg2: argv[1] = $argv[1]"

When I test my scripts on Solaris 8. The results showed as following.

% passarg.env abc cde
passarg: argv[1] = abc argv[2] = cde
passarg1: argv[1] = abc argvp[2]=cde
passarg2: argv[1] = abc

But when I tested it on RHEL 6.5 the results showed as below.

% passarg.env abc cde
passarg: argv[1] = abc argv[2] = cde
passarg1: argv[1] = abc argvp[2]=cde
passarg2: argv[1] = test passing arguments

Ideally, the Linux results should be correct.
Unfortunately, I don't know how to check Solaris built-in csh version
I only found tcsh package info as below.

% pkginfo -l SUNWtcsh
   PKGINST:  SUNWtcsh
      NAME:  Tenex C-shell (tcsh)
  CATEGORY:  system
      ARCH:  sparc
   VERSION:  11.8.0,REV=2000.01.08.18.12
   BASEDIR:  /
    VENDOR:  Sun Microsystems, Inc.
      DESC:  Tenex C-shell (tcsh)
    PSTAMP:  on28-patch20040130230639
  INSTDATE:  Sep 19 2007 16:10
   HOTLINE:  Please contact your local service provider
    STATUS:  completely installed
     FILES:        7 installed pathnames
                   5 shared pathnames
                   5 directories
                   1 executables
                1034 blocks used (approx)

I kept wondering this should be a bug of Solaris 8 C-shell.
Does anybody have idea?
Except upgrading C-shell version, is there any workaround to solve this issue?

Thanks.

source passarg2.env "$str"

is a tcsh extension.

man csh
...
     source [-h] name
man tcsh
...
     source [-h] name [args ...]

In Linux, csh is a link to tcsh.
For consistent behavior you can change the shebang to

#!/bin/tcsh
1 Like

Hi,

Thank you for response.
Just as you said, when I change #!/bin/csh to #!/bin/tcsh , everything is fine.

It seems that if I change all scripts to executable (chmod 755) and replace
"source" command like passarg2.env "$str" the result is also correct.
Does it mean csh allow executable scripts to take arguments?

Yes, because an executable script is a command (just like all external executables).
While "source" shares shell variables, a command is completely separate.

Hi.

Apparently MadeInGermany did a better job of explaining csh / tcsh differences than I did on LinuxForums -- good work.

Observations:

  1. One should avoid csh-like shells for scripting: Csh Programming Considered Harmful in favor of Bourne-shell relatives: bash , ksh , zsh .
    .
  2. One may be able to replace source <file> <args> with ./<file> <args>
    .
  3. Not all Linux have csh as link to tcsh :
$ which csh tcsh
/bin/csh
/usr/bin/tcsh
$ ls -lH $( which csh tcsh )
-rwxr-xr-x 1 root root 143144 Jul 17  2014 /bin/csh*
-rwxr-xr-x 1 root root 395504 Sep  9  2014 /usr/bin/tcsh*

for a system like:

OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.6 (jessie) 
csh - ( /bin/csh, 2016-02-04 )
tcsh 6.18.01

However, glad to see that there was a solution that suited the OP.

Best wishes ... cheers, drl

1 Like

It seems that my issue has been clarified.
Thank you guys.