Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

In order to modify my paths (like LD_LIBRARY_PATH or PYTHONPATH), I first check is the variable exists. If so, I'm concatenating my old value with the new one (separated with a semicolon), else I'm settings my variable to the new value.

NEW_PATH='/path/to/new/path'  
if [ $LD_LIBRARY_PATH ]  
then  
    export LD_LIBRARY_PATH=$NEW_PATH:$LD_LIBRARY_PATH  
else  
    export LD_LIBRARY_PATH=$NEW_PATH  
fi  

It works, but it is a bit clumsy when you have lot of these in script to source : is there a clever trick to make this block a nice one liner ?

Thanks !

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

This syntax works :

export LD_LIBRARY_PATH=$NEW_PATH${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
share|improve this answer
add comment

So maybe this isn't quite what you asked, but at the top you could do:

if [ -z "$LD_LIBRARY_PATH" ]; then
    export LD_LIBRARY_PATH=/some/sane/default
fi

...and then you know it's always set so you can leave off the else above.

share|improve this answer
add comment

Your Answer

 
discard

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.