bash - Linux: How to eval the contents of STDOUT?

07
2014-07
  • vol7ron

    Consider you have a file with some sort of terminal command. How might each line be executed? Can you pipe the output of more into an eval?

    %> more ./foo.txt
    

    Edit:

    After some help/guidance from the comments, I think I should also make note that it's been a while since I've done any shell scripting, so while I say eval I may mean echo or some other way of executing code.

    If foo.txt contains various lines of code, but I want to execute the line that contains echo 'bar bar', I'm looking for a way to do that from, let's say a grep. Something logically similar to:

    grep echo foo.txt | xargs echo
    
  • Answers
  • slhck

    If you just need to evaluate every line of a file, you don't need a complicated eval or stdout redirection. Two easy options:

    • Source the file (source filename.sh or . filename.sh).
    • Make the file executable and run it (chmod +x filename.sh; ./filename.sh)

    If you really need to eval each line of a file in a loop, do it with while:

    while IFS= read -r line; do eval "$line"; done < filename.sh
    

    You can also pipe the output of any command to while:

    grep foo filename.sh | while IFS= read -r line; do eval "$line"; done
    

    If you need to pass something to source (or .), which expects a file name as an argument, you can use process substitution:

    source <( grep foo filename.sh )
    

  • Related Question

    tcsh - Space in Directory Path in $path Variable in Linux
  • Brad

    I am using Red Hat Enterprise 5.3, and I'm trying to add a directory to my $path variable, but it has spaces in it. The $path variable is space delimited, so how do I differentiate a space in a absolute path from a space that separates the paths?


  • Related Answers
  • Javier Badia

    Isn't $PATH colon-delimited? Anyway, you need to escape the spaces with \. If you wanted to have a directory called my dir, you'd do something like this:

    PATH=/bin /usr/bin /home/user/my\ dir /sbin
    

    This path is just an example, don't copy it verbatim.

  • Doug Harris

    It looks like Brad is using csh or tcsh -- these shells have both $path and $PATH. The shell maintains both when you change the one.

    The way to add the directory with spaces to $path:

    % echo $path
    /opt/local/bin /opt/local/sbin /usr/local/bin /usr/local/sbin /Users/dharris/bin
    % echo $PATH
    /opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/Users/dharris/bin
    
    % set path = ($path /tmp/directory\ with\ spaces)
    
    % echo $path
    /opt/local/bin /opt/local/sbin /usr/local/bin /usr/local/sbin /Users/dharris/bin /tmp/directory with spaces
    % echo $PATH
    /opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/Users/dharris/bin:/tmp/directory with spaces
    
  • Brad

    In my case (using csh) the $PATH variable was messing it up and preventing $path from working, so here is a workaround:

    set savePATH = $PATH
    set path = ($path /usr/my\ dir/has\ spaces\ in\ it)
    set PATH = ($savePATH)
    

    Caveat: paths with spaces must be added last, if you add a non-space path to path after this, it will automatically update $PATH and break it again.