Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 4 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? – fronthem Mar 14 '12 at 6:20
cheers! that's a very nice answer. – fronthem 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.