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