Open PDF with Terminal

Hi, I am still very new to this. So please explain even basic stuff :smiley:

I want to open a pdf document. I know that I can open it with
okular [my doc]
but after that my console remains in the “I still have that doc open mode”

And if I for example exit the Terminal also the PDF viewer closes. Is there any way I can open a File independantly from the Terminal but with the Terminal? :smiley:

As the matter of fact, there is, yes, and you can also apply this to other GUI applications. :wink:

The thing is that when you start something from the command line, then the shell will be executing whatever it is that you started as a child process, and will wait for that child process to terminate before it’ll return the prompt to you.

Enter job control. The shell is capable of multitasking, and so you can tell the shell to start a particular job in the background, and simply return the prompt to you. The way to do this is to add a space and a single ampersand ("&") at the end of the command, like so…: :arrow_down:

okular your_document_here &

If you also want to suppress shell error output for the process, then you must route stderr to /dev/null, like so… :arrow_down:

okular your_document_here 2>&1>/dev/null & 

Lastly, if you want to be able to close the terminal window while your PDF viewer remains active, you can try the following ─ including the suppression of stderr output… :arrow_down:

nohup okular your_document_here 2>&1>/dev/null & 

Note that the last character on that line must be a single ampersand, preceded by a space.

You can learn more about shell syntax ─ at least, for GNU Bash ─ from the Advanced Bash-Scripting Guide at The Linux Documentation Project. :wink:

4 Likes

Thats a really nice answer! Thank you :slight_smile:

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.