0

This is how my input string looks like:

INPUT_STRING="{/p1/p2=grabthistext}"

I want to print grabthistext from the INPUT_STRING.

I tried echo "${INPUT_STRING##*=}" which prints grabthistext}

How do I read only grabthistext using parameter expansion expression?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Asdfg
  • 11,362
  • 24
  • 98
  • 175

3 Answers3

5

If you really want a single parameter expansion then you can use:

#!/bin/bash
shopt -s extglob

INPUT_STRING="{/p1/p2=grabthistext}"

echo "${INPUT_STRING//@(*=|\})}"
grabthistext

I would use a bash regex though:

#!/bin/bash

INPUT_STRING="{/p1/p2=grabthistext}"

[[ $INPUT_STRING =~ =(.*)} ]] && echo "${BASH_REMATCH[1]}"
grabthistext
Fravadona
  • 13,917
  • 1
  • 23
  • 35
2
temp="${INPUT_STRING##*=}" 
echo "${temp%\}}"
grabthistext
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
1

You can do it in two steps: first extract the fragment after = as you already did, and store it in a new variable. Then use the same technique to remove the undesired } suffix:

INPUT_STRING="{/p1/p2=grabthistext}"
TEMP_STRING=${INPUT_STRING##*=}
OUTPUT_STRING=${TEMP_STRING%\}}

echo "$OUTPUT_STRING"
# grabthistext

Check it online.

axiac
  • 68,258
  • 9
  • 99
  • 134