1

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.

codeforester
  • 39,467
  • 16
  • 112
  • 140
lukejanicke
  • 977
  • 1
  • 9
  • 25
  • Re: "where Q=2 went" -- it was executed (in the foreground, vs `P=1` happening in the background in a subshell that then exited after completion of that one-line/three-character command). If you run `echo "$Q"` in the same shell, output will be `2`. – Charles Duffy Oct 07 '17 at 18:36

2 Answers2

5

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
  • Yeah, notice that when you call download, you can also use single quotes. Does this solve your question? – Victor Polo De Gyves Montero Oct 07 '17 at 18:08
  • It’s not what I wanted but it seems to be as good as I’m going to get. – lukejanicke Oct 07 '17 at 18:10
  • 1
    @lukejanicke, if you didn't put them around the link in the user's input, how would the shell know the user wasn't asking it to run `download HELLO` in the background, and a separate command `P=1` in the background, and a separate command `P=2` in the foreground? – Charles Duffy Oct 07 '17 at 18:34
  • My question is how to write a function that take the magnet link and puts it in quotes – lukejanicke Oct 07 '17 at 18:50
  • 1
    So you’re saying there isn’t a way? This is just how bash works? I can live with that. I’m just learning. – lukejanicke Oct 07 '17 at 18:52
  • 1
    You generally can't copypaste a url into the shell prompt without making it a valid shell command, but you can definitely write a command that takes a file full of URLs or that prompts you to paste a URL without requiring quotes. – that other guy Oct 07 '17 at 19:05
  • 1
    @lukejanicke Yes, that's just how bash works. And not just bash, any shell that conforms to POSIX shell syntax rules. In any POSIX shell, if a command line contains an `&` that isn't either escaped or inside quotes (or part of a compound syntactic element like `>&`), it'll be parsed as a delimiter between commands rather than as part of a command or its arguments. – Gordon Davisson Oct 07 '17 at 21:09
3

& is a special character in the shell. You need to quote the string containing it:

download 'HELLO&P=1&Q=2'

Otherwise, it's understood as command separator that runs the left hand side in the background.

choroba
  • 231,213
  • 25
  • 204
  • 289