bashbzip2bzip

unzipping bzip file using bash


I am trying to unzip bzip file using bash this way

tmp1 = #(bzcat all.tbz)
echo tmp1 | tar x

But this fails with

tar: Unrecognized archive format
tar: Error exit delayed from previous errors.

But if I do this

bzcat all.tbz | tar x

and that works

What is the problem with my earlier way.

Thanks!


Solution

  • You have numerous syntax mistakes.

    tmp1=$(bzcat all.tbz)
    echo "$tmp1" | tar x
    
    1. Assignments can't have spaces around =.
    2. Use $(...) to execute a command and substitute its output.
    3. Put $ before the variable name when echoing it.
    4. Put " around the variable to prevent word splitting and wildcard expansion of the result.

    But this most likely still won't work because tar files contain null bytes, and bash variables can't hold this character (it's the C string terminator).

    If you just want to capture the error message if there's a failure, you can do:

    tmp1=$((bzcat all.tbz | tar x) 2>&1)
    if [ ! -z "$tmp1" ]
    then echo "$tmp1"
    fi
    

    See Bash script - store stderr in a variable