how to get rid of ' from variable?

hi,

I want to get rid of ' from my variable value in ksh, and I also want to read the value on the right hand side of = into a variable

thanks

Question is not very clear but here is what I think you are after:

$ AJF=\'fred\'; export AJF
$ env | grep AJF
AJF='fred'

We have a variable whose value includes the ' character.

To extract the value from the above do:

$ echo $AJF | awk -F"'" '{ print $2 }'
fred
$

To put the extracted value into a new variable do:

$ BHG=`echo $AJF | awk -F"'" '{ print $2 }'`
echo $BHG
fred
$

thanks.

The second part I mean I have

var=test

and I want to extract the value test, how can I do this?

thanks

This should work too if you have one or more quotes in the variable:

var=$(echo $test | awk '{gsub("\047","")}1')
$ var="'test'"
$ echo $var
'test'

How about:

$ eval var=$var
$ echo $var
test

thanks all for the answers,

i was just hoping to find out if there is a way using built in ksh functionality

thanks

A built in ksh functionality for what? To remove the sigle quote character from a variable? To extract the characters after the = sign?
What version of the Korn Shell are you using: ksh88, ksh93, pdksh?

sorry,

I meant how to find out both. And how can I find out which version I am using of ksh?

thanks

Assuming STRING is "ABC=DEF":

echo $STRING | awk -F"=" '{ print $1 }'

Will give you the text before the equals sign and:

echo $STRING | awk -F"=" '{ print $2 }'

or

echo $STRING | awk -F"=" '{ print $NF }'

will give you the value after the equals sign.

Both ...
With the AT&T ksh93 and mksh(MIRBSD KSH) you can remove the single quote(s) like this:

$ s="a'b'c'"
$ print -- $s           
a'b'c'
$ print -- ${s//\'}
abc

With ksh88 and pdksh I suppose you should use an external command (tr -d ?).

Regarding the second one:

$ s=a=b
$ print -- $s
a=b
$ print ${s%=*}
a
$ print ${s#*=} 
b

If the following command works, you have ksh93:

print ${.sh.version}

If the above command throws an error, you could run this one:

print $KSH_VERSION

It will return not an empty string if you have pdksh or mksh.

If neither of which works, you have ksh88. To get the exact version you'll need to set the editing mode (for example to vi):

set -o vi

... and then press the Esc key, followed by Ctrl+V.

Hope this helps.

many thanks, that helped, and I also found out my version is Version M-11/16/88i :slight_smile: