3

I have the following lines in a .sh shell script:

wget -O discord.deb https://discordapp.com/api/download?platform=linux&format=deb
sudo dpkg -i discord.deb
rm -f discord.deb

For some reason, the script does not work. However, if I run the commands one-by-one manually, the code works as intended. What am I doing wrong?

1 Answers1

2

The problem is that the URL contains &, which tells the shell to run the wget command in the background. The dpkg command is then ran before the file is finished downloading.

To fix, simply add quotation marks around the URL:

wget -O discord.deb 'https://discordapp.com/api/download?platform=linux&format=deb'
sudo dpkg -i discord.deb
rm -f discord.deb
  • 1
    it's generally better to use single-quotes for fixed strings (to prevent interpolation), and double-quotes only where you want variable interpolation to occur. But make sure to check that the quotes you use don't also appear in the URL. BTW, sudo is needed to install the .deb file, but is not needed to rm it. – cas Jan 08 '18 at 01:02
  • Good points. I am used to using "" instead of '' in programming languages where '' denotes a character instead of a string. In your opinion, is it a significant enough of a difference to change the answer? – Aaron Franke Jan 08 '18 at 02:35
  • 1
    It makes no difference for this particular URL, but I'd change it. better to use the most appropriate quoting because someone will copy-paste-edit this answer with a URL where it does matter and then wonder why it doesn't work properly. BTW, this distinction between single and double quotes is pretty common - used in all bourne-type shells, perl, php, javascript and several others (see https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(strings)) – cas Jan 08 '18 at 02:45