BASH Internal : Replace pattern with string without external command

Morning,
I'm trying step up my scripting game .. :rolleyes::confused::smiley:

Is there a way to do the replacement with an or without using an external command ?

I did try but no joy.

var=${var//\(|\)/}
#!/bin/bash
var="lulus.UbiRwidgets.com (10.1.1.1)"
var=${var//\(/}
var=${var//\)/}
echo "$var"

Thanks !!
Have a great day !!
Pop

Do what replacement?

Code which doesn't do what you want doesn't explain what you do want.

Come on, Corona! Having telephatic abilities is part of the job description, no? ;-))

Being the better psychic i conclude you want to extract the IP-address of your variables content, i.e. the part between "(" and ")".

Carefully reread the part of the man page dealing with "${var%%...}" and "${var##...}" - the one cuts off from the beginning of the variable, the other from the end. You basically do your conversion in two steps, first everything up to "(" then everything behind ")".

I won't spoil the fun finding out how it works, therefore the exact implementation is left as an exercise to the reader.

I hope this helps.

bakunin

Advanced Bash Scripting Guide - String Operations

Sorry fellas,

Being in the expert bucket I figured you guys would "see" it. ... and if you execute the script I provided you will see that it actually works and the desired output is shown. No mind reading necessary !

I am trying to remove the "(" and the ")" from var without using an external command in a single step.

You posted that it didn't work. I took your word for it.

echo "${VAR//[()]/}"
2 Likes