Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I want to use regex in bash at line of variable assignment


oldip_a="" << How to use regex to del all '\'? and assign to oldip_a

Do you have any idea?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

To remove the \, use bash parameter substitution.
// means replace all ... (a single / would mean replace only the first)
//\\ means replace all \ (backslash) chars
/} means replace with nothing (There is nothing bewtween / and the closing }

echo "${ip//\\/}"


Or, if there are many occurrences \. to do, in a file with many such IP addresses, you can assign each modified value to an array item.

ip=($(printf '
' | sed 's/\\//g'))
for ((i=0;i<${#ip[@]};i++));do
  echo "${ip[i]}"


?! .. I just noticed that your second data item is a reversal of the first.. Is that what you want as your output? (probably not, but it got me wondering)..

share|improve this answer
How to use regex in echo? Are you can explain to me? – asdf Mar 14 '12 at 6:20
cheers! that's a very nice answer. – asdf Mar 14 '12 at 6:45
@Peter.O : can you please explain me what {#ip[@]} specifies? – pradeepchhetri Mar 14 '12 at 7:25
Regarding the many case: “If parameter is an array variable subscripted with @ or *, the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.” – man bash. ip=('' '14\.130\.31\.172' '33\.135\.220\.0'); echo "${ip[@]//\\/}" – manatwork Mar 14 '12 at 10:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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