I am trying to use while loop in csh
shell command prompt in RHEL 7.2 but am getting the below error:
$ while true
while: Expression Syntax.
The same is working in bash
shell.
cshlinux
I am trying to use while loop in csh
shell command prompt in RHEL 7.2 but am getting the below error:
$ while true
while: Expression Syntax.
The same is working in bash
shell.
Best Answer
The syntax of
while
loops incsh
is different from that of Bourne-like shells. It's:When
csh
is interactive, for some reason, thatend
has to appear on its own on a line.For the arithmetic-expression to test on the success of a command, you need
{ cmd }
(spaces are required).{ cmd }
in arithmetic expressions resolves to 1 if the command succeeded (exited with a 0 exit status) or 0 otherwise (if the command exited with a non-zero exit status).So:
But that would be a bit silly especially considering that
true
is not a built-in command incsh
. For an infinite loop, you'd rather use:By contrast, in POSIX shells, the syntax is:
And if you want the condition to evaluate an arithmetic expression, you need to run a command that evaluates them like
expr
, orksh
'slet
/((...))
or thetest
/[
command combined with$((...))
arithmetic expansions.