Recursively add folders to export command on bashrc

0

I want to add recursively add folders to an exported variable. Suppose I have the MATLABPATH variable declared below:

export MATLABPATH=PATH1:PATH2:…PATHN:$MATLABPATH

And I want to add all folders inside the folder RECURSIVEFOLDER to MATLABPATH variable. How I do that in some automatic way?

Thanks.

Werner

Posted 2013-08-04T05:08:33.237

Reputation: 205

Answers

1

Use find to list the directories:

export MATLABPATH="$(find RECURSIVEFOLDER -type d -printf %p:)$MATLABPATH"

The -printf %p: causes each directory to be printed with a : after it, so the find command's output will always end with a :. Provided $MATLABPATH wasn't empty before running this command, that will work fine.

Note that the above will add RECURSIVEFOLDER to the path, as well as its recursive children. If you didn't want that, add -mindepth 1 to the find command, if your find command implements -mindepth, or replace RECURSIVEFOLDER with RECURSIVEFOLDER/*.

rici

Posted 2013-08-04T05:08:33.237

Reputation: 3 493

You may add -mindepth 1 to find command to not add the RECURSIVEFOLDER x). Thanks x) – Werner – 2013-08-04T15:07:24.873