Skip to main content
added 156 characters in body
Source Link
Marian
  • 156
  • 3

For a pure bash solution (however, it's bash-specific): If $x is the variable containing your string:

expr length "$x2="${x//[^\"]/}"
echo ${#x2}

The ${x// thing removes all chars except ", expr length${#x2} then countscalculates the characterslength of the resulting stringthis rest.

(Original suggestion using expr which has problems, see comments: )

expr length "${x//[^\"]/}"

If $x is the variable containing your string:

expr length "${x//[^\"]/}"

The ${x// thing removes all chars except ", expr length then counts the characters of the resulting string.

For a pure bash solution (however, it's bash-specific): If $x is the variable containing your string:

x2="${x//[^\"]/}"
echo ${#x2}

The ${x// thing removes all chars except ", ${#x2} calculates the length of this rest.

(Original suggestion using expr which has problems, see comments: )

expr length "${x//[^\"]/}"
Source Link
Marian
  • 156
  • 3

If $x is the variable containing your string:

expr length "${x//[^\"]/}"

The ${x// thing removes all chars except ", expr length then counts the characters of the resulting string.