I have two variables $dc_list and $TEST_VENUE . If I do echo in bash

echo $dc_list



Now I need to fetch the value for TEST_VENUE from dc_list.

I tried this:

dc=$(echo "$dc_list" | sed -e 's/^.*'"$TEST_VENUE"':"\([^"]*\)".*$/\1/')  ---> This is not working

But If I hardcode,

dc=$(echo "$dc_list" | sed -e 's/^.*"standby-production-dc":"\([^"]*\)".*$/\1/')  ---> This works.

But I want to achieve from First sed. Any suggesttions for this?

  • Is that some sort of json output?
    – Jeff Schaller
    Commented Aug 20, 2016 at 0:23
  • 2
    this appears to be a string representation of an associative array (json or otherwise) which you are attempting to manipulate in bash. bash can natively handle associative arrays. so why not consider building your "dc_list" as a bash associative array?: artificialworlds.net/blog/2012/10/17/… Commented Aug 20, 2016 at 0:45

3 Answers 3


For bash you can use parameter expansion

  • Great. Is it possible to get this in single line. I am trying to get this done in a single line. Commented Aug 24, 2016 at 15:23

This works for me:

tr '"' _ <<< $dc_list | sed -n "s/.*${TEST_VENUE}_:_\([^_]*\).*/\1/p"

However, my advice would be don't use this, especially if the format of the $dc_list changes.
I recommend using a json parser. I would use something like PHP function json_decode() but obviously there are other alternatives - probably python has something good too.

This example of using PHP's json_decode() works for me and would be far more reliable, of course it assumes you have a PHP cli SAPI available to you.
You could use this code directly in your shell script, I tested using zsh, but I expect it would work for bash too.

<< 'EOF' php -- "$dc_list" "$TEST_VENUE"
$a = json_decode($argv[1]);
echo $a->{$argv[2]} . PHP_EOL;

For JSON data, use a JSON parser such as

$ jq -r ".[\"$TEST_VENUE\"]" <<< "$dc_list" 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .