0

I have a string num1="0123456789" num2="56"

I need to get the substring 0123456. I am trying to do

echo "${string#$num2}"

which gives me, 01234 but it doesn't give me 56. Is there any straight forward way to do this without adding something like

substring="${string#$num2}"
result=substring+num2

Thanks.

Aditya Jha
  • 69
  • 1
  • 8

1 Answers1

2

bash replacement can be used:

substring="${string/$num2*/$num2}"

Test:

$ string="0123456789"
$ num2="56"
$ substring="${string/$num2*/$num2}"
$ echo "${substring}"
0123456
Kubator
  • 1,373
  • 4
  • 13
  • 1
    You may use the `%` to see if the pattern is at the end.. The question is not very clear about it though.. – sjsam Apr 09 '19 at 10:42
  • That's correct thanks for pointing out: if ${var/%pat/repl} then pattern must occur at end of value – Kubator Apr 09 '19 at 10:56