0

I have this environment variable

echo $DT_CUSTOM_PROP

returns

APPCODE=IK22 ENVIRONMENT=DEV APPLICATION=xxxxx-xxxxx-xxxxx-xxxxx ANOTHERKEY=ANOTHERVALUE

How can I get APPLICATION from it?

So that if I do echo $APPLICATION I get xxxxx-xxxxx-xxxxx-xxxxx

Amin Ba
  • 1,603
  • 1
  • 13
  • 38
  • 1
    what does `echo "$DT_CUSTOM_PROP"` return? (ie. are the spaces in your original output really spaces?) – jhnc Jun 27 '22 at 16:20
  • The same: `APPCODE=IK22 ENVIRONMENT=DEV APPLICATION=xxxxx-xxxxx-xxxxx-xxxxx ANOTHERKEY=ANOTHERVALUE ` – Amin Ba Jun 27 '22 at 16:23
  • Is `DT_CUSTOM_PROP` set by something you trust? If so, it looks like you could do: `APPLICATION=$(eval $DT_CUSTOM_PROP; echo $APPLICATION)` or even just `eval $DT_CUSTOM_PROP` – jhnc Jun 27 '22 at 16:25
  • yes. let me check – Amin Ba Jun 27 '22 at 16:27
  • That works. Thanks! What I want to get only the application? – Amin Ba Jun 27 '22 at 16:29

1 Answers1

0

Given:

DT_CUSTOM_PROP='APPCODE=IK22 ENVIRONMENT=DEV APPLICATION=xxxxx-xxxxx-xxxxx-xxxxx ANOTHERKEY=ANOTHERVALUE'

You can do it with parameter expansion in a 2-step process:

tmp=${DT_CUSTOM_PROP#*APPLICATION=}
application=${tmp%% *}    # ==> xxxxx-xxxxx-xxxxx-xxxxx

Or with bash regex matching

if [[ $DT_CUSTOM_PROP =~ "APPLICATION="([^[:blank:]]+) ]]; then
    application=${BASH_REMATCH[1]}
else
    application="not found"
fi
glenn jackman
  • 238,783
  • 38
  • 220
  • 352