Home > front end >  how to remove a directory entry in LD_LIBRARY_PATH variable? (in bash, when the entry is in the midd
how to remove a directory entry in LD_LIBRARY_PATH variable? (in bash, when the entry is in the midd

Time:05-25

Suppose my LD_LIBRARY_PATH is now dir_a:dir_b:dir_c:prj/foo/build:dir_d or dir_a:dir_b:dir_c:prj/foo1/build:dir_d. By using bash script, I want to examin this LD_LIBRARY_PATH and if there is a directory path containing pattern foo*/build I want to remove this foo*/build part. How can I do it?
If this *foo*/build is the last part of LD_LIBRARY_PATH, I know I can remove it by export xx=${xx%:*foo*/build} but I don't know how to do it if this *foo*/build part is surrounded by :s.

CodePudding user response:

LD_LIBRARY_PATH=$( tr : $'\n' <<<$LD_LIBRARY_PATH | grep -v YOURPATTERN | paste -s -d : )

where YOURPATTERN would be a simple regex describing the component you want to be remove. Depending on its complexity, you may consider the options -E, -F or -P of course.

The tr splits the path into segments. The grep removes the unwanted ones. The paste reassembles.

CodePudding user response:

This should do it:

export LD_LIBRARY_PATH=$(echo $LD_LIBRARY_PATH | sed 's/foo.*\/build//g')
  •  Tags:  
  • bash
  • Related