7

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]?

Byte Commander
  • 110,243

2 Answers2

4

Fairly easy to accomplish with xargs and bash's nohup and & commands. The exact command is this:

xargs bash -c 'nohup firefox $@ &' null  < tabs.txt 

With a tests file like this:

$ cat tabs.txt                                         
google.com
askubuntu.com
unix.stackexchange.com

enter image description here

The way this works is simple:

  • redirect tabs.txt file as input stream to xargs.
  • xargs will treat all items coming in as arguments and pass them on to bash -c '' command. The null string is used just to set argument $0. It could literally be any string and is not relevant.
  • bash will run nohup firefox $@ & , while substituting all arguments in place of $@. The final result is that we get to open firefox, and disown it from bash's process via nohup and &
3

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.

UTF-8
  • 5,910
  • 10
  • 34
  • 68