1

I have a complex grep/awk/etc command line which use some " some $var already, which makes it even impossible to use

VAR="$( that command )" 

to get all output

I don't want to create temp files, which make it even ugly,

is it possible to pass pipe output into a variable in bash

like

command   |   > $VAR
3
  • command > $VAR would create a file named $VAR. Commented Aug 23, 2013 at 8:22
  • Have you tried to escape the " and $ characters inside the VAR="$(..."? Example: VAR="$(echo \")" ; echo $VAR Commented Aug 23, 2013 at 8:22
  • are you sure you can't use : VAR="$( that command )"? Notice that inside $(...), you "go" into that level, which means you don't have to escape quotes. Ex: you can write VAR="$(echo "toto titi")" instead of VAR="$(echo \"toto titi\")". Using the first form ( VAR="$(echo "toto titi")" ) You'll end up (after bash evaluate first the $(...) part) with VAR="toto titi", as needed. Commented Aug 23, 2013 at 11:19

2 Answers 2

2

Just use the : VAR=$(complex command ), writing complex command exactly as you would if you were writing it on the next line.

Ex: if you have

foo=1
bar="2 3"
awk -v myfoo="$foo" -v mybar="$bar" '..... complex 
                                          awk 
                                          script here .....'

you could put that into VAR with:

foo=1
bar="2 3"
VAR="$( awk -v myfoo="$foo" -v mybar="$bar" '..... complex 
                                                   awk 
                                                   script here .....' )"

ie, once inside $(...), bash is reading things as if it was at the "first level". It works as $(...) is evaluated first before the line containing it is evaluated.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do a for loop as following (Just a warning, for loop split output by space. So you'll got a word by iteration):

    #!/usr/bin/ksh

    for RC_OUTPUT in $(./testEcho.ksh)
    do
        echo $RC_OUTPUT
    done

Here a more complex solution (max line lenght = 80): #!/usr/bin/ksh

    LINE_LENGTH_MAX=80
    LINE_LENGTH=0

    for RC_OUTPUT in $(./testEcho.ksh)
    do
        CURRENT_LENGTH="$(expr length $RC_OUTPUT)" 
        (( LINE_LENGTH = LINE_LENGTH + CURRENT_LENGTH + 1 ))
        if [[ $LINE_LENGTH -gt $LINE_LENGTH_MAX ]]
        then
            LINE_LENGTH=$CURRENT_LENGTH
            echo $RC_OUTPUT
        else
            echo -n "$RC_OUTPUT "
        fi
    done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.