Search code examples
bashshelladb

An issue with a variable in adb shell


In bash, when I type:

$ echo 1 | while read L; do echo $L; done

Obviously I got 1 as a result.

Within adb shell, I got the same result.

However, if I type as follows:

$ adb shell "echo 1 | while read L; do echo $L; done"

I got nothing!

Is there anyone who knows why I got nothing in the above?


Solution

  • You should have got a newline from the adb shell. The trouble is that the shell invoking the adb shell "… echo $L; … line saw the $L and expanded it to the empty string, so the adb shell saw the command line:

    echo 1 | while read L; do echo; done
    

    Use single quotes:

    adb shell 'echo 1 | while read L; do echo $L; done'
    

    Alternatively, get inventive:

    L='$L'
    adb shell "echo 1 | while read L; do echo $L; done"
    

    Now the invoking shell replaces the $L with $L and everything works as intended (but rather coincidentally).