how to execute lines coming from a grep result?

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

Assumptions:

  • 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:

tmp=$(mktemp)
{
    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
)

Tags:

Bash

Grep