0

I have a very long line in my .bash_profile, as below:

export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/cuda/lib:$HOME/anaconda2/lib:/usr/local/lib:/usr/lib:/opt/intel/compilers_and_libraries/mac/lib:/opt/intel/mkl/lib:$DYLD_FALLBACK_LIBRARY_PATH

How do I break this line into multiple lines?

Also, is .bash_profile considered a shell script? If not, what exactly is it categorized as? If I know this, I will be able to look up general formatting guide for this file.

  • 2
    that's an unusual item at the end of the list; are you perhaps missing a $ to make it $DYLD_FALLBACK_LIBRARY_PATH ? – Jeff Schaller Jul 25 '18 at 19:31
  • @JeffSchaller Yes you are right. A $ sign is missing. – Ben Jul 27 '18 at 12:55
2
DYLD_FALLBACK_LIBRARY_PATH="/opt/intel/mkl/lib:$DYLD_FALLBACK_LIBRARY_PATH"
DYLD_FALLBACK_LIBRARY_PATH="/opt/intel/compilers_and_libraries/mac/lib:$DYLD_FALLBACK_LIBRARY_PATH"
DYLD_FALLBACK_LIBRARY_PATH="/usr/local/lib:/usr/lib:$DYLD_FALLBACK_LIBRARY_PATH"
DYLD_FALLBACK_LIBRARY_PATH="/usr/local/cuda/lib:$HOME/anaconda2/lib:$DYLD_FALLBACK_LIBRARY_PATH"
export DYLD_FALLBACK_LIBRARY_PATH

Or, with an array and string concatenation:

paths=(
    /usr/local/cuda/lib
    "$HOME/anaconda2/lib"
    /usr/local/lib
    /usr/lib
    /opt/intel/compilers_and_libraries/mac/lib
    /opt/intel/mkl/lib
    ${DYLD_FALLBACK_LIBRARY_PATH:+"$DYLD_FALLBACK_LIBRARY_PATH"}
)

oldIFS="$IFS"; IFS=:
export DYLD_FALLBACK_LIBRARY_PATH="${paths[*]}"
IFS="$oldIFS"; unset oldIFS

That last item in the array will expand to the value of the DYLD_FALLBACK_LIBRARY_PATH variable, or to nothing if it's not set or is empty.

The expansion of "${paths[*]}" will be the paths joined into a single string delimited by the first character of the IFS variable.

1

You can do this using the line break character: \. Also you should declare and export on two different lines so:

DYLD_FALLBACK_LIBRARY_PATH=/usr/local/cuda/lib:\
$HOME/anaconda2/lib:\
/usr/local/lib:/usr/lib:\
/opt/intel/compilers_and_libraries/mac/lib:\
/opt/intel/mkl/lib:\
$DYLD_FALLBACK_LIBRARY_PATH
export DYLD_FALLBACK_LIBRARY_PATH

Your bash profile is a config file and a script of sorts.

  • I would consider ~/.bash_profile a script, as it certainly executes commands. – Jeff Schaller Jul 25 '18 at 19:28
  • @JeffSchaller: Fair enough. Updated. – Jesse_b Jul 25 '18 at 19:30
  • If I was going to break up the line, I'd break each colon-separated element to its own line, just for legibility. – Jeff Schaller Jul 25 '18 at 19:32
  • @Jesse_b Should there be a $ sign before the DYLD_FALLBACK_LIBRARY_PATH variable at the end of the declaration? – Ben Jul 27 '18 at 13:02
  • @Ben: Yes, you didn't have it in the original question so I left it the way it was presented. – Jesse_b Jul 27 '18 at 13:19
0

Since it's Bash, you can append to a string (var+=value). Though you pretty much need a temporary variable here, since you're prefixing the paths to the original value.

tmp=/usr/local/cuda/lib
tmp+=:$HOME/anaconda2/lib
tmp+=:/usr/local/lib
tmp+=:/usr/lib
tmp+=:/opt/intel/compilers_and_libraries/mac/lib
tmp+=:/opt/intel/mkl/lib
export DYLD_FALLBACK_LIBRARY_PATH=$tmp:$DYLD_FALLBACK_LIBRARY_PATH
unset tmp

Note that like your original snippet, this assumes that DYLD_FALLBACK_LIBRARY_PATH is not empty in the beginning. If it is, this'd leave a trailing : in it.


Alternatively, if your paths don't contain whitespace just put the string in quotes, and remove the whitespace afterwards:

export DYLD_FALLBACK_LIBRARY_PATH="/usr/local/cuda/lib:
   $HOME/anaconda2/lib:
   /usr/local/lib:/usr/lib:
   /opt/intel/compilers_and_libraries/mac/lib:
   /opt/intel/mkl/lib:
   $DYLD_FALLBACK_LIBRARY_PATH"
DYLD_FALLBACK_LIBRARY_PATH=${DYLD_FALLBACK_LIBRARY_PATH//[[:space:]]}

This is a bit silly:

prepend() { declare -n _n=$1; _n="$2$_n"; }
prepend DYLD_FALLBACK_LIBRARY_PATH /opt/intel/mkl/lib:
prepend DYLD_FALLBACK_LIBRARY_PATH /opt/intel/compilers_and_libraries/mac/lib:
prepend DYLD_FALLBACK_LIBRARY_PATH /usr/lib:
prepend DYLD_FALLBACK_LIBRARY_PATH /usr/local/lib:
prepend DYLD_FALLBACK_LIBRARY_PATH "$HOME/anaconda2/lib:"
prepend DYLD_FALLBACK_LIBRARY_PATH /usr/local/cuda/lib:
export DYLD_FALLBACK_LIBRARY_PATH

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.