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
  • 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, 2018 at 19:31
  • @JeffSchaller Yes you are right. A $ sign is missing.
    – Michael
    Jul 27, 2018 at 12:55

4 Answers 4

3
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"}
)

IFS=:$IFS
export DYLD_FALLBACK_LIBRARY_PATH="${paths[*]}"
IFS=${IFS#?}

That last item in the array will expand to the value of the DYLD_FALLBACK_LIBRARY_PATH variable, or 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, which we temporarily set to :.

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.

5
  • I would consider ~/.bash_profile a script, as it certainly executes commands.
    – Jeff Schaller
    Jul 25, 2018 at 19:28
  • @JeffSchaller: Fair enough. Updated.
    – jesse_b
    Jul 25, 2018 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, 2018 at 19:32
  • @Jesse_b Should there be a $ sign before the DYLD_FALLBACK_LIBRARY_PATH variable at the end of the declaration?
    – Michael
    Jul 27, 2018 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, 2018 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
-1

I use Stephen Collyer's bash_path_funcs, described in Linux Journal way back in 2000:

https://www.linuxjournal.com/article/3645 https://www.linuxjournal.com/article/3768 https://www.linuxjournal.com/article/3935

The addpath function adds an entry to a path only if it is not there in the first place. delpath -n deletes all non-existent directories from a path.

You can get the pathfunc.tgz file from https://web.archive.org/web/20061210054813/http://www.netspinner.co.uk:80/Downloads/pathfunc.tgz

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.