Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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/*.

share|improve this answer
    
You may add -mindepth 1 to find command to not add the RECURSIVEFOLDER x). Thanks x) –  Werner Aug 4 '13 at 15:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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