Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 940
tjwrona1992 Avatar asked Dec 08 '17 21:12

tjwrona1992


People also ask

How do I add to an environment variable?

On Windows 7, just type "system" in the META-Prompt and you'll see an entry "Edit the System Environment Variables". From there, click "Environment variables". There, you can either edit the system variable PATH (bottom list) or add/edit a new PATH variable to the user environment variables.

How do I add items to my PATH environment variable?

To add a path to the PATH environment variableIn the System dialog box, click Advanced system settings. On the Advanced tab of the System Properties dialog box, click Environment Variables. In the System Variables box of the Environment Variables dialog box, scroll to Path and select it.

How do you pass an environment variable in a running jar?

Depends on what value you would like to pass as part of environment variable. If its just key=value pair where value is not location path , then you just use -D option , just like how you do VM Args and run. Save this answer.

How do you append a PATH variable in Linux?

We can add a new path for all users on a Unix-like system by creating a file ending in . sh in /etc/profile. d/ and adding our export command to this file. All of the scripts in /etc/profile.


1 Answers

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
like image 60
eternauta3k Avatar answered Oct 08 '22 13:10

eternauta3k