Menu

brash vs GNU bash: pipes are a little more convenient in brash

2016-05-05
2016-05-06
  • Lowell Boggs, Jr.

    Using Pipes to Populate Variables

    When writing scripts in GNU Bash and the bourne shell, there is an annoyance with regard to the storing the results of a pipe into a variable: The read command, which has an option to let you do the job, is not usable.

    Here's an example:

    #Bash/bourne shell example:
    
       someProgram | read fred
    
       # you can't use fred here because the read 
       # occurred in a different shell
    

    As the comments in the example explain, when a pipe ends in a read command, the results are wasted because the read executes as part of the pipe.

    In brash, however, the final stage of a pipe operation occur in the current shell environment, so in the example above, the first line of output from someProgram is actually found int he variable, "fred".

    Note also that brash adds the -f option to the read command, that lets you read the entire file into a string and store it in fred.

    In releases prior to 1.2.0, the read command does not support the -a option which will allow the file to be read into an array variable. But the -f option does let you at least get the data.

    For example:

    # brash example
        someProgram | read -f vars
    
        echo "$vars" | 
        while read line
        do
                echo "the next line from someProgram's output is:  $line"
        done
    
     

    Last edit: Lowell Boggs, Jr. 2016-05-05
  • Lowell Boggs, Jr.

    Coming soon in release 1.2.0 bo brash, the -a option will be added to the read command to allow it to read the lines of a file into an array variable.

     

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.