Interpreting $? output in bash shell

Posted on 2011-10-08
Last Modified: 2012-05-12
Hello, I am running a command using pipe, say

echo stats | nc 1001
echo $?

Open in new window

and then I am getting the output of $? as 0. so here $? is depends on which command? echo or nc?? for getting $? 0 does both command needs to be executed successfully or either of nc or echo would do? how does $? gets evaluated in piping. Thanks!
Question by:beer9
    LVL 17

    Accepted Solution

    At least in Bash, the last command is relevant for the return value ... for example:

    dustpuppy:~ # ls a
    /bin/ls: a: No such file or directory
    dustpuppy:~ # echo $?
    dustpuppy:~ # ls a | cat
    /bin/ls: a: No such file or directory
    dustpuppy:~ # echo $?

    Open in new window

    The pipe itself does not evaluate any result codes, but only makes sure the actual stdio stuff is done ...
    LVL 48

    Assisted Solution

    $? is always the status of the last command, so in your case it is the 'nc' command.

    If you want to get the status of a command in a pipe (from memory, only supported by bash), you can inspect the PIPESTATUS array.

    For example, if you do

    ls | foobar | wc

    Open in new window

    $? will contain 0 (exit status of wc)

    ${PIPESTATUS[1]}  will contain value >0  (exit status of foobar)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
    The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now