I was trying to execute the following shell script below

function Check_Status () {

       if [[ "$(adb shell getprop sys.boot_completed)" =~ "adb: no devices/emulators found" ]]; 
          echo "here"

            echo "im here"


I'm getting the following output, and I'm expecting to see "here" not "I'm here"


Not sure what could be missing

  • 1
    Please don't post images of text. Copy and paste the text itself into your question and format it as code by selecting it and pressing Ctrl-K or by using the editor's {} icon. – cas Jul 19 at 13:27
  • Try installing shellcheck and check the script syntax. Did you include bash shebang (#!/bin/bash) in the beginning of the script? – Artur Meinild Jul 19 at 13:33
  • no i didnt include that, im not so much familiar with it anyway – Amr Kamel Jul 19 at 13:38
  • 1
    @ArturMeinild the syntax is fine here, shellcheck wouldn't help. – terdon Jul 19 at 13:41

The text on your pic looks the same as the one in your script, yes. But it's a bit hard to be sure from just a pic.

But note how the text comes to your terminal when you run the script? The command substitution is supposed to capture the output, whatever gets caught by it, does not get printed. adb probably prints that message to the standard error, not standard output, so it doesn't get caught.

You could verify that with something like this:

echo "running the command substitution... (errors would print after this line)"
output=$(adb shell getprop sys.boot_completed)
echo "captured output (stdout): '$output'"

Then see what comes out where.

And if that's indeed the problem, then you need to redirect the stderr to stdout in the command substitution:

if [[ "$(adb shell getprop sys.boot_completed 2>&1)" =~ "adb: no devices/emulators found" ]]; then
  • here is my output running the command substitution... adb: no devices/emulators found captured output '', I'm not sure why it doesn't catch the output – Amr Kamel Jul 19 at 13:44
  • 2>&1 solved my issue, could you explain what it means – Amr Kamel Jul 19 at 13:46
  • 1
    @AmrKamel, Unix processes have two distinct output streams, the "standard output" stream that's meant for data, and which gets delivered via pipes and captured by command substitutions, and the "standard error" stream that usually bypasses those. See e.g. en.wikipedia.org/wiki/Standard_streams So, apparently adb treats that situation as an error, and the message to standard output. – ilkkachu Jul 19 at 13:56
  • 1
    @AmrKamel see How to redirect stderr to a file – terdon Jul 19 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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