user496934
user496934

Reputation: 4020

Manipulate string in shell script

I am writing a shell script for some purpose. I have a variable of the form --

var1 = "policy=set policy"

Now I need to manipulate the variable var to get the string after index =. That is I should have "set policy". Also I need to to this for many other variables where the value of "=" is not constant. Like

var2 = "bgroup = set bgroup port"
var3 = "utm = set security utm" 

Can you give me an idea how to do it, please?

Upvotes: 0

Views: 1050

Answers (2)

kurumi
kurumi

Reputation: 25599

Other ways that is not dependent on what shell you have.

$ var1="policy=set policy"
$ echo $var1 | awk '{sub(/.[^=]*=/,"")}1'
set policy
$ echo $var1 | cut -d= -f2-
set policy
$ echo $var1 | ruby -e 'puts gets.split(/=/,2)[1]'
set policy
$ echo $var1 | sed 's/.[^=]*=//'
set policy

Upvotes: 0

Rafe Kettler
Rafe Kettler

Reputation: 76945

${var#*=}

removes the shortest match of *= from the left. Note that this is not in place: if you want to save the result, you'll have to store the result in a variable.

On a side note, this is for bash. AFAIK it also works for ksh and zsh, but not csh or tcsh.

Upvotes: 3

Related Questions