Search code examples
basharia2

Calling a Bash function with an argument that has an ampersand in it


I want a bash function that turns this …

download HELLO&P=1&Q=2

into this …

aria2c -d ~/Downloads "HELLO&P=1&Q=2"

HELLO&P=1&Q=2 is a MAGNET link. If it doesn’t have double quotes around it, aria2c doesn’t work. But I can’t seem to keep it all together. The problem seems to be that the MAGNET link has series of &key=value pairs at the end (in my example &P=1&Q=2) and these seem to be getting stripped out and separated from the rest of the link (i.e. HELLO).

Here is my .bash_profile function.

download() {
  aria2c -d ~/Downloads "$1"
}

Here are is a sample output.

~ $ download HELLO&P=1&Q=2
[1] 7389
[2] 7390
aria2c -d /Users/lukejanicke/Downloads HELLO
[1]-  Done                    download HELLO
[2]+  Done                    P=1
~ $ 

No even sure where Q=2 went. With real MAGNET links, all the parts are displayed in this enumerated way.

I need some way of capturing the whole MAGNET link as a single parameter and then executing the aria2c command with quotes around the link.


Solution

  • Double quotes make bash to interpret the inner string.

    I've tested it with:

    $ download() {   aria2c -d ~/Downloads "$1"; }
    victor 0 12:48 /Users/victor/tmp
    
    $ download "http://localhost:8080/HELLO&P=1&Q=2"
    
    10/07 12:48:56 [NOTICE] Downloading 1 item(s)
    
    10/07 12:48:56 [ERROR] CUID#7 - Download aborted. 
    URI=http://localhost:8080/HELLO&P=1&Q=2E