Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got a text file with certain installation instructions, and I know I can grep for a unique occurrence in the file. For example, the text file has something like:

MYDIR=`find /home/user -name somedir`

In bash, how can I execute the lines after a grep in the file? Something like:

execute result from "grep somedir INSTALLFILE"
execute result from "grep 'export PERL5LIB' INSTALLFILE"
share|improve this question
Isn't automatic execution in this fashion a teensy bit dangerous? – Faheem Mitha Jun 30 '11 at 13:35
@Faheem and @avilella: More than a teensy bit, rather dangerous indeed! Also, somewhat fragile. – Caleb Jun 30 '11 at 19:21
up vote 2 down vote accepted


  • you have control over this file and are not in danger of malicious code
  • you want to set these variables your current shell

You could redirect your commands to a temp file and execute that:

    grep somedir INSTALLFILE
    grep 'export PERL5LIB' INSTALLFILE
} > "$tmp"
. "$tmp"

Or you could evaluate the results

eval "$(grep somedir INSTALLFILE)"
eval "$(grep 'export PERL5LIB' INSTALLFILE)"

Updating an old answer. What I would do today is use a process substitution:

source <(
    grep somedir INSTALLFILE
    grep 'export PERL5LIB' INSTALLFILE
share|improve this answer
Note that for source <(...), you need bash 4 or above. It didn't work with older versions (zsh was OK though). – Stéphane Chazelas Feb 5 at 16:29

To evaluate the lines in a separate shell process:

grep somedir INSTALLFILE | sh

To evaluate the lines in the running shell process:

eval "$(grep somedir INSTALLFILE)"
share|improve this answer
+1 for being the the only one to see the forest through the trees. Hereby deleting my answer. @avilella: for convenience you can use -A 1 or -B 1 as flags on one or the other of your greps to fetch both lines with one grep and use that in either of @Gilles solutions. – Caleb Jun 30 '11 at 21:20

Have you considered using the 'exec' feature of find as in:

find somedir -name INSTALLFILE -exec {}\;
share|improve this answer
I think the OP is looking to execute the results that come back grom grep, not the run every result that comes back from find. – Caleb Jun 30 '11 at 13:29

Your Answer


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.