0

I know this is very similar to the question How can I strip first X characters from string using sed?

But I am having trouble storing the output string as a variable in my bash script

myString='Foo: 1234'
echo "$myString"| sed -r 's/^.{5}//'

produces the desired result yet it is not stored in variable.... when i try:

myVariable1=$('"$myString"| sed -r 's/^.{5}//'')
myVariable2='"$myString"| sed -r 's/^.{5}//''
myVariable3=$(""$myString"| sed -r 's/^.{5}//'")
myVariable4=""$myString"| sed -r 's/^.{5}//'"

anyone care to show me where I'm going wrong please

Community
  • 1
  • 1
Fuzzybear
  • 1,388
  • 2
  • 25
  • 42

2 Answers2

2

You don't need sed for this. bash can do it.

$ myString='Foo: 1234'
$ myVar=${myString:5}
$ echo $myVar
1234
anishsane
  • 20,270
  • 5
  • 40
  • 73
  • yes i can confirm that this worked on the example you show thank you, yet when I try on my actual string it does not!! is there special characters i need to watch out for this way?? my string is: 'block.GetHash = 21f909a7d7efb8c7b0f6bee1f518bf1bbb7651b999b8e7cd6a2edc8f785b65a5' – Fuzzybear Jul 21 '13 at 14:12
  • $ test='block.GetHash = 21f909a7d7efb8c7b0f6bee1f518bf1bbb7651b999b8e7cd6a2edc8f785b65a5' ---- $ echo ${test:5} .GetHash = 21f909a7d7efb8c7b0f6bee1f518bf1bbb7651b999b8e7cd6a2edc8f785b65a5 – anishsane Jul 21 '13 at 14:41
1

You can just keep the echo:

myVar=$(echo "$myString"| sed -r 's/^.{5}//')

or use a here-string:

myVar=$(sed -r 's/^.{5}//' <<<"$myString")
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175