I am trying to remove the first n characters from a string I get the following error :

line 5: ${ echo $data | sed -r 's/.{ $index0 }//' }: bad substitution

when using this code from within a bash script :

data=${ echo $data | sed -r 's/.{$index}//' }

any correction please ?



  1. Command substitution is $( ... ), not ${ ... }. Regular parenthesis, not curly braces.
  2. You have $index within single-quotes, so it will not be expanded by the shell. You need double-quotes for that.
  3. Put double-quotes around $data in the echo, it makes a difference if data contains whitespace or glob characters
  4. In bash, you can just use the substring expansion data=${data:$index} to drop a number of characters from the front of the string.

Suggested reading: http://mywiki.wooledge.org/BashGuide

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.