I'm trying to open a PDF using Preview where the input file is a shell var:

$ pdf=$(wget -qO- 'http://website.com/file.pdf') && open "$pdf"

$ pdf=$(wget -qO- 'http://website.com/file.pdf') && open -a /Applications/Preview.app -f "$pdf"

$ open -a /Applications/Preview.app $(wget -qO- 'http://website.com/file.pdf')

$ pdf=$(wget -qO- 'http://website.com/file.pdf') | open -a /Applications/Preview.app -f -

etc etc etc

I always get the same error:

-bash: /usr/bin/open: Argument list too long

Is it possible to do this?

example PDF for anyone who wants to try:


3 Answers 3


Cool problem showing how amazing bash, pipes and open are. You are so close on the fourth try:

wget -qO- 'https://www.com/big.pdf' | open -f -a Preview.app

Luckily the -a argument lets the -f take the data from stdin but not open in TextEdit.

  • Right back at you, @lily - I had fun trying lots of half efforts before checking the man page and hoping the two options would work.
    – bmike
    May 12, 2018 at 3:18
  • This answer isn't complete, the question refers specifically to opening a PDF using Preview where the input file is a shell var. May 12, 2018 at 3:29
  • I'm really impressed -- I would not have expected this to work, but it does! May 12, 2018 at 4:05

You can open a PDF as a Bash variable (expressed as a path to the file) like this.

  1. Set your variable:

  2. Open the file like this:

    open "$pdf" -a Preview

However, your example indicates that the file doesn't exist on your filesystem. If you want to open a raw data stream stored in a variable, I recommend echoing your variable, and piping it to open using the -f option like this.

  1. Define your variable:

    pdf=$(wget -qO- https://www.exploit-db.com/docs/english/44592-linux-restricted-shell-bypass-guide.pdf)
  2. Open the raw data as a stream from standard input:

    echo $pdf | open -f -a Preview

Explanation: If you store the output of wget -qO- … as a variable, as opposed to downloading the file onto your filesystem, open won't know what to do as you're passing it the raw file data rather than the path to the file. In man open ‘file’ refers to an openable file for the given application, the variable in your example is not that. The -f option is useful in this scenario as your method of setting the variable involves storing the entire file stream, which won't be interpreted as a file by open, and therefore it can only be opened as a file stream.

  • 2
    This won't work because it will corrupt the file in several ways. First, variables are not suitable for binary data, especially anything that contains null characters (zero bytes). Second, using a variable reference without double-quotes around it will subject the value to word splitting and wildcard expansion. Finally, echo adds a newline to the end of what it print. You can solve the last two problems using printf "%s" "$pdf" instead of echo $pdf, but the first is harder to solve. You'd have to do something like storing it in base64 encoding. May 12, 2018 at 4:04
  • Thanks for the feedback, Gordon. I did not know that about variables and null data. So effectively what the question is originally trying to achieve isn't possible? May 12, 2018 at 11:14
  • I should mention though, that this code does indeed run successfully for this example. May 12, 2018 at 11:18

The same thing @bmike suggested but without wget which isn't native to OS X

curl -so- 'https://www.example.com/some.pdf' | open -f -a Preview.app

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .