Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Unset or empty variable test in (t)csh

9 views
Skip to first unread message

Hauke Fath

unread,
Nov 19, 2021, 12:05:39 PM11/19/21
to
Hi,

what is the proper {t,}csh way to test if a variable is either unset, or
empty?

The naive

if ( ! ${?TERM} || ${TERM} == "" ) ...

errors out in the second expression, for obvious reasons. While tcsh
does do short circuit expressions, it attempts to expand variables
beforehand.

Cheerio,
Hauke


ObHint: " | sed '/csh.*harmful/d'"

--
Now without signature.

Kenny McCormack

unread,
Nov 19, 2021, 12:29:40 PM11/19/21
to
In article <1piwk1d.1qnfocmjw81h7N%dont.spa...@googlemail.com>,
Hauke Fath <ha...@Espresso.Rhein-Neckar.DE> wrote:
>Hi,
>
>what is the proper {t,}csh way to test if a variable is either unset, or
>empty?
>
>The naive
>
>if ( ! ${?TERM} || ${TERM} == "" ) ...
>
>errors out in the second expression, for obvious reasons. While tcsh
>does do short circuit expressions, it attempts to expand variables
>beforehand.

You have to do it step by step. For example, the following works for me:

if $?TERM then
if $TERM == "" then
echo "TERM is set but empty"
else
echo "TERM is set to: $TERM"
endif
else
echo "TERM is unset"
endif


>ObHint: " | sed '/csh.*harmful/d'"

Well done!

But, yeah, the syntax of csh/tcsh is kinda creaky, but that's mainly because
it hasn't been maintained (i.e., enhanced, built up, etc) in decades. It
pretty much is what it was in the 1980s. But the fact is, if you use tcsh
as your interactive shell, as I still do, you still need to be able to edit
your startup files and stuff. So, you still need to keeping de old skills
alive!

--
In politics and in life, ignorance is not a virtue.
-- Barack Obama --

Hauke Fath

unread,
Nov 19, 2021, 1:01:23 PM11/19/21
to
Kenny McCormack <gaz...@shell.xmission.com> wrote:

> Hauke Fath <ha...@Espresso.Rhein-Neckar.DE> wrote:
> >if ( ! ${?TERM} || ${TERM} == "" ) ...
> >
> >errors out in the second expression, [...]
>
> You have to do it step by step. For example, the following works for me:
>
> if $?TERM then
> if $TERM == "" then
> echo "TERM is set but empty"
> else
> echo "TERM is set to: $TERM"
> endif
> else
> echo "TERM is unset"
> endif

The problem is, this does not give you 'TERM is either unset, or empty'
in one branch.

For the background, the test is from csh.login where I want to run
tset(1) only if TERM is not sane, to avoid side-effects. Since I just
learned that "eval `tset -QIs ...`" will only produce a string, and not
re-initialize the terminal, I am now running tset unconditionally.

> >ObHint: " | sed '/csh.*harmful/d'"
>
> Well done!

From the grab bag of "The [t]csh user's hundred helpful sed one-liners"

[1] ;)

> But, yeah, the syntax of csh/tcsh is kinda creaky, but that's mainly because
> it hasn't been maintained (i.e., enhanced, built up, etc) in decades.

AFAIK csh has a handwritten parser, no defined grammar. Many things are
just as irregular as they are, take 'em or leave 'em.

Cheerio,
Hauke


[1] Also: <https://meyerweb.com/eric/comment/chech.html>

--
Now without signature.

Hauke Fath

unread,
Nov 19, 2021, 1:30:28 PM11/19/21
to
Hauke Fath <dont.spa...@googlemail.com> wrote:

> The naive
>
> if ( ! ${?TERM} || ${TERM} == "" ) ...
>
> errors out in the second expression

FTR, I just came across [1] whose

if ( ! ${?TERM} || { eval `if ( ${TERM} == "" ) exit 1` } ) ...

fits my bill.

Cheerio,
Hauke


[1]
<https://stackoverflow.com/questions/13343392/how-to-check-if-an-environment-variable-is-either-unset-or-set-to-the-empty-stri>


--
Now without signature.

Kenny McCormack

unread,
Nov 19, 2021, 1:30:45 PM11/19/21
to
In article <1piwmwf.1pl1i9s16nqowbN%dont.spa...@googlemail.com>,
Hauke Fath <ha...@Espresso.Rhein-Neckar.DE> wrote:
...
>The problem is, this does not give you 'TERM is either unset, or empty'
>in one branch.
>
>For the background, the test is from csh.login where I want to run
>tset(1) only if TERM is not sane, to avoid side-effects. Since I just
>learned that "eval `tset -QIs ...`" will only produce a string, and not
>re-initialize the terminal, I am now running tset unconditionally.

How about:

if ! $?TERM set TERM=""
if $TERM == "" echo "TERM is unset or empty"

ObCrabbingAboutCSH: One thing that does annoy me is that "set" requires an
equals sign, but "setenv" does not. And by "requires", I mean it only
works one way. You have to always remember to use an "=" with "set" and to
NOT use one with "setenv".

--
Alice was something of a handful to her father, Theodore Roosevelt. He was
once asked by a visiting dignitary about parenting his spitfire of a daughter
and he replied, "I can be President of the United States, or I can control
Alice. I cannot possibly do both."

Kenny McCormack

unread,
Nov 19, 2021, 1:32:49 PM11/19/21
to
In article <1piwodk.rj170lk47a9sN%dont.spa...@googlemail.com>,
Hauke Fath <ha...@Espresso.Rhein-Neckar.DE> wrote:
...
>FTR, I just came across [1] whose
>
>if ( ! ${?TERM} || { eval `if ( ${TERM} == "" ) exit 1` } ) ...
>
>fits my bill.

Wow. I had no idea...

--
People often ask what is the difference between liberals and conservatives.
It is this. Libs see the government helping them and are OK with the government
also helping other people. Cons see the government screwing them and are OK with
that as long as the government is also screwing other people.

Hauke Fath

unread,
Nov 19, 2021, 1:35:57 PM11/19/21
to
Hauke Fath <dont.spa...@googlemail.com> wrote:

> if ( ! ${?TERM} || { eval `if ( ${TERM} == "" ) exit 1` } ) ...

Alternatively[1],

if ( ! ${?TERM} ) set TERM = ""

then continue to check for an empty string.

So many ways to skin a cat...

Cheerio,
Hauke


[1]
<https://unix.stackexchange.com/questions/197497/how-to-check-if-string-is-blank-in-tcsh>

--
Now without signature.

Kenny McCormack

unread,
Nov 19, 2021, 1:38:33 PM11/19/21
to
In article <1piwoyj.v7briefpmumsN%dont.spa...@googlemail.com>,
Hauke Fath <ha...@Espresso.Rhein-Neckar.DE> wrote:
>Hauke Fath <dont.spa...@googlemail.com> wrote:
>
>> if ( ! ${?TERM} || { eval `if ( ${TERM} == "" ) exit 1` } ) ...
>
>Alternatively[1],
>
>if ( ! ${?TERM} ) set TERM = ""
>
>then continue to check for an empty string.

I take it, you posted that w/o having read my most recent post on this
thread?

--
Q: How much do dead batteries cost?

A: Nothing. They are free of charge.

Hauke Fath

unread,
Nov 19, 2021, 3:41:11 PM11/19/21
to
Kenny McCormack <gaz...@shell.xmission.com> wrote:

> >if ( ! ${?TERM} ) set TERM = ""
> >
> >then continue to check for an empty string.
>
> I take it, you posted that w/o having read my most recent post on this
> thread?

Right - our postings crossed each other. :)

USENET was not designed for chat mode...

Cheerio,
Hauke

--
Now without signature.

Kenny McCormack

unread,
Nov 19, 2021, 4:31:50 PM11/19/21
to
In article <1piwuuz.o88pa8a90bmmN%dont.spa...@googlemail.com>,
Hauke Fath <ha...@Espresso.Rhein-Neckar.DE> wrote:
>Kenny McCormack <gaz...@shell.xmission.com> wrote:
>
>> >if ( ! ${?TERM} ) set TERM = ""
>> >
>> >then continue to check for an empty string.
>>
>> I take it, you posted that w/o having read my most recent post on this
>> thread?
>
>Right - our postings crossed each other. :)
>
>USENET was not designed for chat mode...

Yup. So true.

--
Atheism:
It's like being the only sober person in the car, and nobody will let you drive.
0 new messages