variables in a script

Hi,

I have a variable (var) which can be any number. I would like to use the cut command on a string depending on the variable.

eg
var=4
echo $string | cut -c 1-$var

This doesn't work as cut sees $var as $var rather than as 4. Can anyone suggest how I can evaluate $var in the cut command so cut sees $var as 4 ?

Many thanks
Helen:confused:

The exact code that you posted works fine for me. I can't imagine why you're having trouble with it.

Hi Perderabo,

I get "cut: The list arguments following the c option are not correct." from my script.

Although I did a quick command line test and it worked ok.

You must have some other problem. Maybe "var" has some other value. Do a:
echo var = $var
before the cut statement.

As the code stands in my script I'm doing the following

string2=`echo $string | cut -c1-c$length`

I did an echo of the whole line just before I executed the line

ie
echo "string2=\`echo $string | cut -c1-c$length\`"
string2=`echo $string | cut -c1-c$length`

and ran the script again

I got

string2=echo 524286918 | cut -c1-c6
cut: The list arguments following the c option are not correct.

The line which was output is how I wanted it to look but I still get the error message

I've tried running it both under sh and ksh

it's a bizzare one!

You want "cut -c1-$length". Lose that "c" directly in front of the variable.

Thanks Perderabo
Can't believe I missed that - I think I've been working far too many hours! :wink:

removed...