Read a URL from a file and open it in a Firefox tab

user569041

PreTabs.txt is a file I have on my desktop, and it just contains the line

google.com

I want to pipe the content of that file into Firefox so that it will open the URL in a new tab.

If you execute cat PreTabs.txt | firefox,
shouldn't that be the same as firefox [Contents of PreTabs.txt]?

UTF-8

Piping is not the same as passing an argument to a program.

If you run a program, a process is created. I'm simplifying here, but basically every process has a standard in and a standard out. It receives input through the standard in (maybe) and produces output at its standard out (maybe). These are abbreviated stdin and stdout respectively.

When you execute a program in the terminal, its standard in is what you type into the terminal while its running. Its standard out is printed on the terminal.

What you do when you execute a | b is to execute both a and b but connect the stdout of a's process to the stdin of b's process. That is, if a's process created output, it's fed as input to b's process, just like if you were to execute both a and b in separate terminals and type everything a's process prints on its terminal into the terminal you're running b on. Well, almost. You can only do text, for example, whereas processes can pipe arbitrary data to a different process.

You can test this by piping data to tee.

Example:

echo foo | tee

echo reads its arguments and writes them to its stdout. Its stdout is connected to tee's stdin. Tee simply reads from its stdin on a per-line basis and writes its input to its stdout. This means that "foo" will be printed to your terminal as that's the stdout of tee's process.

echo foo | tee | tee | tee

Has the same effect, of course.

You can do more useful stuff which is pretty simple, too:

echo foo | sha256sum

This calculates and prints the sha256sum of "foo\n". Where \n is a line feed because echo ends its output in a line feed, otherwise the previous example wouldn't have worked because tee wouldn't have seen a line feed.

So your command cat PreTabs.txt | firefox executes cat with the argument PreTabs.txt which causes cat to write the contents to its stdout. This stdout is the stdin of firefox which means that firefox can read it. It doesn't have to and there is no rule of how it should behave on this. It looks like it either doesn't read it or reads it and then ignores it.

If you want to pass an argument to a program but that argument is inside a text file, you can use backticks. Commands between paired backticks are executed in advance and their output is pasted at that point in the outer command.

Example:

echo bar > foo
echo `cat foo` > foobar

The first line writes "bar" into the file foo (overwrites the file's contents if it already exists, otherwise creates a new file). In the second line, cat foo is executed first. It reads its parameter foo and learns that it's supposed to read foo's contents and print them onto its stdout. This stdout is then pasted where

`cat foo`

is in the second line, so the second line becomes

echo foo > foobar

which writes foo into the file foobar.

This actually causes Firefox do to something:

echo "stackexchange.com" > foo
firefox `cat foo`

opens a new tab loading http://stackexchange.com in Firefox.

You said that in your case it loads the file whose name it got as a parameter and I actually have a vague recollection of it having behaved like that in the past. It's probably a better idea to tell it explicitly what to do:

echo "stackexchange.com" > foo
firefox -new-tab `cat foo`

This is defined in the man page (man firefox) as:

   -new-tab url
          Open url in a new tab.

though it might be different for your Firefox version, of course.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to open an URL from command line to Firefox and "switch to tab"?

From Dev

How to open an URL from command line to Firefox and "switch to tab"?

From Dev

Preventing Firefox from replacing "open URL" with "Switch to tab"

From Dev

Open URL in current tab in Firefox via Applescript

From Dev

Firefox: open history in a tab

From Dev

Using protractor to open file url with Firefox

From Dev

Open and read from excel file

From Dev

Open and read from excel file

From Dev

Read in String From File, Split by Tab in Ruby

From Dev

How to get the URL from a firefox tab from the terminal?

From Dev

How to get the URL from a firefox tab from the terminal?

From Dev

Firefox open new tab behaviour

From Dev

Get URL of current active tab from Firefox via command line

From Dev

Get URL of current active tab from Firefox via command line

From Dev

Read Domains from Text File and Open in Browser

From Dev

Open file to read from web form

From Dev

open file in new tab

From Dev

Open URL in new tab from IPython Notebook/Jupyter cell

From Dev

How to open url in new tab from JavaScript script

From Dev

How to open the previous url from the history in a new tab in Vimperator

From Dev

Open App from URL works on Firefox for Android but not on Google Chrome

From Dev

How to open doc file in android from url

From Dev

How to open .csv file from a url with Python?

From Dev

xdg-open URL from text file

From Dev

How to open doc file in android from url

From Dev

Android: Open json file from url for Realm

From Dev

How to open file from storage by URL address?

From Dev

Links from apps always open a file:// URL

From Dev

Force a URL to open in a new tab

Related Related

HotTag

Archive