Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am writing a csh alias so that I can use the following bash function in my csh :

function up( )
    for ((i=1; i <= LIMIT; i++))
    cd $P
    export MPWD=$P

(I stole the above bash function from here)

I have written this:

alias up 'set LIMIT=$1; set P=$PWD; set counter = LIMIT;  while[counter!=0] set counter = counter-1; P=$P/.. ; end cd $P; setenv MPWD=$P'

However, I am getting the following error:

while[counter!=0]: No match.
P=/net/devstorage/home/rghosh/..: Command not found.
end: Too many arguments.

and my script is not working as intended. I have been reading up on csh from here.

I am not an expert in csh and what I have written above is my first csh script. Please let me know what I am doing wrong.

share|improve this question

I’m not sure it’s even possible to do a while loop all on one line.  But there are several fixable errors:

  • while[counter!=0] should be while ($counter != 1) (note the spaces before and after the “!=”).
  • set counter = counter-1 should be @ counter = $counter – 1 (note the spaces before and after the ‘’).
  • P=$P/.. should be set P=$P/.. –– or better yet, set P="$P/.." (in case $P contains space(s).
  • Which reminds me, set P=$PWD should be set P="$PWD", cd $P should be cd "$P", and setenv MPWD=$P should be setenv MPWD="$P", all for the same reason.
  • And, if you do manage to get the while loop all on one line to work, you’re probably going to need a ; after the end.
share|improve this answer
I had to write the while loop in a single line because I was getting a horde of errors while using the the next line facility using / – Wildling Jan 22 '13 at 5:26
OK, but my point is: have you ever gotten a ‘while loop in a single line’ to work? If yes, please post an example; even if it’s something trivial like mkdir –p foo/foo/foo; while { cd foo } pwd; end. – Scott Jan 22 '13 at 16:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.