1

On the command line, if I type

adb -s "" shell

I get a shell, but if I try to do this in bash:

#!/bin/bash
ADB_ID=""
adb -s ${ADB_ID} shell

I get an error. I understand that it's not passing in any content for ${ADB_ID}. I've tried escaping the quotes, which results in it looking for the device named "", which is incorrect. I've also tried using single quotes and single escaped quotes, which are both wrong. How can I pass the command line equivalent in my bash script?

Maxthecat
  • 1,250
  • 17
  • 37

2 Answers2

6

Get into the habit of using double quotes around your variables, (almost) always:

adb -s "$ADB_ID" shell

is what you want.

Quoting is shell programming is a much-discussed topic. I won't get into the details here except to say:

  • the shell uses does certain expansions, including variable expansion, before it tokenizes the line into words: the command and its parameters.
  • the shell uses sequences of whitespace to separate words
  • if you don't quote the variable above, the shell will see this:

    adb -s  shell
    

    and it has no way to know that there should be something between "-s" and "shell".
    With quotes, the shell sees

    adb -s "" shell
    

    and it is obvious that there is a zero-length word there.

For more research, https://stackoverflow.com/tags/bash/info is a good place to start.

For this specific issue, BashPitfalls numbers 2 through 5.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • What do double quotes do in this context? Also, is using double quotes around variables something specific for the `adb` command? – Neb May 15 '19 at 16:44
  • Thanks! That does the trick! @Neb, I looked into it after glenn posted his answer. Check out https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable – Maxthecat May 15 '19 at 16:46
  • "Basically" the command-prompt got rid of the quotes when it processed in `adb -s "" shell`, leaving an empty argument. here, we are effectively adding back the equivalent quotes in `adb -s "$ADB_ID" shell`. – Gem Taylor May 15 '19 at 17:08
  • 2
    @Neb In this context, the double-quotes mean "expand the variable reference, but don't mess with the result". Without quotes, the variable's value would be split into "words" (which in this case is what makes it vanish, because there are zero words in the value), and then each word that contains anything that might be a wildcard gets expanded to a list of matching files. You usually don't want either of these things, so you should put double-quotes around variable references to prevent them. – Gordon Davisson May 15 '19 at 17:24
0

Always quote variables, unless you know exactly what you're doing.

adb -s "$ADB_ID" shell

The shell expands variables then collects the command line arguments, so if there's an unquoted null variable ($ADB_ID), it gets ignored. When you quote a null variable ("$ADB_ID"), that's the same as passing a null string as an argument.

wjandrea
  • 28,235
  • 9
  • 60
  • 81