I want to write a bash function to both start downloading a file and open it in a video player; I don't know how to get the path of the file being downloaded though:

function dl-and-stream() {
  aria2c "$1" &
  sleep 30
  mpv #What to put here

PS: This is not my only use case, and I’m aware of --out, but I like to keep the original file name.

Update: My use cases can be fulfilled via --on-download-complete (and other event handlers), but I still prefer not using that if possible. (Because they need the creation of additional script files.)

  • Why do you start arai2c in the background and start mpv before aria2c has even figured out how to access the URL and possibly followed some redirections?
    – PerlDuck
    Dec 16, 2018 at 18:28
  • @PerlDuck Ik, I just wanted to show a simple example. I actually use youtube-dl for this function, and keep retrying mpv till it successfully opens the file.
    – HappyFace
    Dec 16, 2018 at 18:32
  • If you use youtube-dl then you should probably ask about youtube-dl and not about some other tool that is not so widely known. Consider we figure out that aria2c writes the output filename to /etc/var/log/foo/output.file.name and we'd give you the advice to read that file. Of what help would that be for youtube-dl?
    – PerlDuck
    Dec 16, 2018 at 18:42
  • @PerlDuck I don’t have a problem with this particular function (because I know how to get the path from youtube-dl😂), it’s just a simple example to illustrate the question. And you should totally try aria2, it’s an incredible download manager. I have used prefer much all famous download managers, and a lot of not famous ones. Aria2 is just in its own league. :)
    – HappyFace
    Dec 16, 2018 at 18:50

1 Answer 1


aria2c provides an --on-download-start option which lets you run a command as soon as the download started. To use it to start playing the file, write a script with

mpv "$3"

and run aria2c like that in your function:

aria2c --on-download-start /path/to/script "$1"

That has the extra advantage that you don’t need to sleep for a fixed amount of time, which may not be enough in some cases. In fact, you don’t need a function at all with this solution, a simple alias does it:

alias dl-and-stream='aria2c --on-download-start /path/to/script'
  • Thanks, but unfortunately aria2 renames the files sometimes.
    – HappyFace
    Dec 16, 2018 at 19:55
  • @HappyFace How? Please include this information in your question!
    – dessert
    Dec 16, 2018 at 19:57
  • It’s downloading files from the internet, and pretty much all downloaders sometimes don’t just use everything from the last slash onwards. I don’t know the details of how they do it.
    – HappyFace
    Dec 16, 2018 at 20:05
  • I don’t have an example at hand, but this textual approach won’t be able to deal with them, since the new name is gotten from the server.
    – HappyFace
    Dec 16, 2018 at 20:07
  • 1
    Apparently aria2c calls the command quoted, that’s really sloppy. Unfortunately that prevents something like --on-download-start 'sh -c "mpv \"$2\""'. If you already knew and tried stuff, why didn’t you add this information to your question? Please do so!
    – dessert
    Dec 16, 2018 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.