1

This question already has an answer here:

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?

marked as duplicate by Jeff Schaller, cas, Community Jan 8 '18 at 2:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I’m impressed that those lines work in an interactive shell. What shell do you use? – Jeff Schaller Jan 8 '18 at 0:43
  • @Jeff My question and answer is shorter and simpler than that post. The point was to post a simple question and answer in one post. I don't think this deserves removal or anything. – Aaron Franke Jan 8 '18 at 1:07
  • I'm not suggesting deletion; it's the same problem & solution, so identifying the duplicate is the right thing to do. unix.stackexchange.com/help/duplicates – Jeff Schaller Jan 8 '18 at 1:45
  • I'm still curious how you ran it interactively and didn't get a background process. – Jeff Schaller Jan 8 '18 at 1:45
  • I did get a background process, but it also posted terminal output, and if I manually waited for it to finish, then I could run dpkg and it would work. – Aaron Franke Jan 8 '18 at 2:29
1

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 8 '18 at 1: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 8 '18 at 2: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 en.wikipedia.org/wiki/…) – cas Jan 8 '18 at 2:45

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