Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7165731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:17:56+00:00 2026-05-28T14:17:56+00:00

This browser gist by defunkt github user starts with this shell expression if [

  • 0

This browser gist by defunkt github user starts with this shell expression

if [ -t 0 ]; then ...

What is the meaning of this line of code?

UPDATE: could you also explain why i need this check before doing anything else?

For the sake of completeness, here is the entire little script (it allow to pipe text to the default browser):

 if [ -t 0 ]; then
  if [ -n "$1" ]; then  
    open $1
  else
    cat <<usage
Usage: browser
       pipe html to a browser

$ echo '<h1>hi mom!</h1>' | browser
$ ron -5 man/rip.5.ron | browser
usage

fi
else
  f="/tmp/browser.$RANDOM.html"
  cat /dev/stdin > $f
  open $f
fi
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:17:57+00:00Added an answer on May 28, 2026 at 2:17 pm
    • [ and ] invokes test
    • -t makes the test test a file descriptor to see if it’s a terminal
    • 0 is the file descriptor for STDIN.

    So that says

    if STDIN is a terminal then ...
    

    Motivation

    I’d have to read the whole script to know for sure, but usually it’s because the script wants to do something visually slick like clear the screen, or prompt interactively. If you’re reading a pipe, there’s no point in doing that.

    Detail

    Okay, let’s examine the whole script:

    # If this has a terminal for STDIN
    if [ -t 0 ]; then
      # then if argument 1 is not empty
      if [ -n "$1" ]; then  
        # then open whatever is named by the argument
        open $1
      else
        # otherwise send the usage message to STDOUT
        cat <<usage
    Usage: browser
           pipe html to a browser
    
    $ echo '<h1>hi mom!</h1>' | browser
    $ ron -5 man/rip.5.ron | browser
    usage
    #That's the end of the usage message; the '<<usage'
    #makes this a "here" document.
    fi  # end if -n $1
    else
      # This is NOT a terminal now
      # create a file in /tmp with the name
      # "browser."<some random number>".html"
      f="/tmp/browser.$RANDOM.html"
      # copy the contents of whatever IS on stdin to that file
      cat /dev/stdin > $f
      # open that file.
      open $f
    fi
    

    So this is checking to see if you’re on a terminal; if so it looks for an argument with a file name or URL. If it isn’t a terminal, then it tries to display the input as html.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've written up a minimal example of this. The code is posted here: https://gist.github.com/1524725
How to make this css code cross browser compatible using jquery. this code only
can anyone explain me this weird browser behavior: gist link It seems to be,
I was wondering if somebody could shed some light on this browser behaviour: I
Using this gist to prevent links from launching a browser when the site is
My question is not like this one: Browser-independent way to detect when image has
The browser does this by calling public void emulateShiftHeld() method on the WebView which
I have some JSON returned to the browser like this product: { Title: School
i am trying to automate the gui in c# .Is this same as browser
This is what my browser sent, when logging into some site: POST http://www.some.site/login.php HTTP/1.0

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.