Simple way to append to an environment variable that may not exist yet in csh
Asked Answered
C

2

11

I am trying to append a path to the end of my PERL5LIB environment variable, but I am not sure that this variable will always exist. If it doesn't exist I would like to simply initialize it to the value that I am trying to append.

Here is an example that works:

if ( $?PERL5LIB ) then
    setenv PERL5LIB ${PERL5LIB}:/some/other/path
else
    setenv PERL5LIB /some/other/path
endif

While this works, it still seems pretty clunky since I have to basically write the same line twice. What I would like to do is come up with a more efficient one line solution (possibly using parameter expansion).

Is there a way to consolidate this to one line? (Or a couple lines that don't involve writing out "/some/other/path" multiple times)


For example this could be done in bash:

export PERL5LIB=${PERL5LIB:+${PERL5LIB}:}/some/other/path
Catholicism answered 8/12, 2017 at 21:55 Comment(2)
P.S i can seee edit revisions to see your last edit stole my answer!Vilhelmina
The missing question mark was a typo. The code in the question doesn't work at all without the question mark, but I didn't realize I left it out until I went to write my own answer.Catholicism
L
2

If the environment variable is not defined, set it to an empty string. This avoids duplication.

if ( ! $?PERL5LIB ) then
        setenv PERL5LIB ""
else
        setenv PERL5LIB "${PERL5LIB}:"
endif
setenv PERL5LIB ${PERL5LIB}/some/other/path
Lunarian answered 1/10, 2018 at 12:54 Comment(2)
So simple... Yet somehow I could not come up with this on my own haha.Catholicism
-1 this is wrong. if the variable was not defined, it will now be defined as :/some/other/path which includes two paths, the first of which is the empty path. this can be interpreted by some programs as . which can be very bad.Victorious
C
0

For lack of a better answer I will post a slightly improved version of what I had in the question. The following at least eliminates writing out the path twice...

set perl5lib = "/some/other/path"
if ( $?PERL5LIB ) then
    setenv PERL5LIB ${PERL5LIB}:${perl5lib}
else
    setenv PERL5LIB ${perl5lib}
endif

Although it's only a very slight improvement, at least it's something.

EDIT:

Technically this could be shortened to:

set perl5lib = "/some/other/path"
[ $?PERL5LIB ] && setenv PERL5LIB ${PERL5LIB}:${perl5lib} || setenv PERL5LIB ${perl5lib}

Not the most readable, but I guess that's about as good as I'm going to get.

EDIT 2:

Possibly more readable? ...I don't really know.

set perl5lib = "/some/other/path"
[ $?PERL5LIB ] \
  && setenv PERL5LIB ${PERL5LIB}:${perl5lib} \
  || setenv PERL5LIB ${perl5lib}
Catholicism answered 31/1, 2018 at 15:57 Comment(4)
I'm accepting this as the answer for lack of a better one... If you have a better answer please feel free to post it and I will change the accepted answer.Catholicism
you do know the use of curly brackets is unnecessary and adds complexity, { = Start in-line expansion and } = End in-line expansion since your not doing any expression you should not be using them and are wasting performance I think you don't know the difference between complexity and size in computers.Vilhelmina
@MartinBarker This is more about code duplication than actual complexity and performance. I just don't want to write "/some/other/path" more than once because when you do that for many environment variables the chance of having a typo in one and not knowing about it becomes much greater.Catholicism
Also I personally think adding curly brackets improves readability which is more important than performance in this case.Catholicism

© 2022 - 2024 — McMap. All rights reserved.