In following shell, error shows LD_LIBRARY_PATH: unbound variable if the LD_LIBRARY_PATH not set.
Can I use similar usage like ${xxx:-yyy} to simplified it.
#!/bin/bash
set -o nounset
export LD_LIBRARY_PATH=/mypath:$LD_LIBRARY_PATH
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could use this construct:
Explanation:
If
LD_LIBRARY_PATHis not set, then${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}expands to nothing without evaluating$LD_LIBRARY_PATH, thus the result is equivalent toexport LD_LIBRARY_PATH=/mypathand no error is raised.If
LD_LIBRARY_PATHis already set, then${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}expands to:$LD_LIBRARY_PATH, thus the result is equivalent toexport LD_LIBRARY_PATH=/mypath:$LD_LIBRARY_PATH.See the Bash Reference Manual / 3.5.3 Shell Parameter Expansion for more information on these expansions.
This is an important security practice as two adjacent colons or a trailing/leading colon count as adding the current directory to
$PATHor$LD_LIBRARY_PATH. See also: