0

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?

3
  • 3
    Please accept more answers. Also, be aware that you cannot have a space before or after the assignment operator in most shells. Commented Apr 17, 2011 at 6:57
  • 1
    The 'too localized' close voter needs to get out of their own shell and into the real world. This is a question about shell programming and fully on topic for SO. Commented Apr 17, 2011 at 7:03
  • Which shell are you scripting? Commented Apr 17, 2011 at 7:21

2 Answers 2

3
${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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is valid POSIX shell code. (*csh are not POSIX shell implementations.)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.