executing Linux commands from shell - do I need to append /dev/null?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tvnaidu
    Contributor
    • Oct 2009
    • 365

    #1

    executing Linux commands from shell - do I need to append /dev/null?

    I have follwoing commands in shell script, do I need to append ">> /dev/null" for each command to execute from shell?.

    $ cat scr.sh
    #!/bin/bash
    cp /mnt/flash/data/valgrind_l.tar .
    cp /mnt/flash/data/pkg.tar .
    tar -xvf valgrind_l.tar
    tar -xvf pkg.tar
    cd ..
    cp /mnt/flash/data/bin.tar .
    tar -xvf bin.tar
    export PATH=$PATH:/usr/local/bin
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    You don't *need* to, but it might be a good idea.

    appending "> /dev/null" will make the output from each command not show on the terminal (it goes to /dev/null, which is like it throwing it away).

    This it typically done in shell scripts to keep the terminal from filling up with piles and piles of output that probably does not matter to the user.

    You might also want to redirect stderr as well so the user doesn't have to see error messages either.

    Code:
    tar -xvf pkg.tar > /dev/null 2>&1
    the extra "2>&1" at the end says to redirect stderr into stdout (which is being redirected to /dev/null)

    By the way, using ">" vs ">>" does not matter in this case, but in general ">" truncates a file and then writes output to it (any previous content will be lost) but ">>" appends to a file without losing it's content.

    Comment

    Working...