Shayan
Shayan

Reputation: 868

Command substitution when it involves environment path

I run this command to list my android emulators:

emulator -list-avds

The output is:

Pixel_2_API_28

Now I can run the emulator using this command:

emulator -avd Pixel_2_API_28

Which works just fine. But I want to make an alias for this so that I can run the first found emulator with just one word.

emulator -avd "$(emulator -list-avds)"

But it errors: PANIC: Cannot find AVD system path. Please define ANDROID_SDK_ROOT. But the path is already defined, as you saw, the command when run without command substitution, executes just fine.

So I googled a lot and tried a lot of things to get this working. I tried combinations of eval and choosing the first row with awk 'NR==1{print $1}' and tried putting double quotes and single quotes and escaping the double quotes \" but nothing worked.

I tried storing the command in a variable and inspecting it:

a="$(emulator -list-avds)" ; declare -p a

And the variable looks weird to me:

"eclare -- a="Pixel_2_API_28

output

Maybe this is why it's not working. I have no idea what to google now and how to take this any further, I'd appreciate any help.

Upvotes: 0

Views: 28

Answers (1)

chepner
chepner

Reputation: 532053

The output of emulator includes a DOS line ending (carriage-return/linefeed pair). The command substitution only strips the trailing linefeed, leaving the carriage return as part of the string passed as an argument to the next call.

To strip it, you can use tr:

emulator -avd "$(emulator -list-avds | tr -d '\r')"

Upvotes: 1

Related Questions