Operating System - HP-UX
1833847 Members
2348 Online
110063 Solutions
New Discussion

Re: Can I use a environment variable as a number

 
SOLVED
Go to solution
Pascal Kouknas
Occasional Contributor

Can I use a environment variable as a number

What I would like it to do is.

#export SEQ=21003
#echo $SEQ
#21003

#export var33=$SEQ+1
#echo $var33
#21004


What it is doing is:

#export var33=$SEQ+1
#echo $var33
#21003+1


Is there a simple way to do this. Treat it as a number ?
4 REPLIES 4
Sridhar Bhaskarla
Honored Contributor

Re: Can I use a environment variable as a number

You can do it by using command substitution

Try this way

export var33=$(($SEQ + 1))

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sajid_1
Honored Contributor

Re: Can I use a environment variable as a number

hello,

the best way for doing arithmatic calculation is 'expr'. Chcek:
# man expr

this way you can define the numbers..
learn unix ..
Michael Tully
Honored Contributor
Solution

Re: Can I use a environment variable as a number

Try using 'expr'

$ var33=`expr $SEQ + 1`
$ echo $var33
21004
Anyone for a Mutiny ?
A. Clay Stephenson
Acclaimed Contributor

Re: Can I use a environment variable as a number

As long as this is the POSIX or Korn shell, you can do it within the shell and it is more efficient than calling expr because no fork/exec's are necessary.

var33=$((${SEQ} + 1))
or
let var33=${SEQ}+1

THese two statements are equivalent but the first form is really the preferred syntax because it is very tolerant of spaces. It is also easier to group complex statements within paren's.


If it ain't broke, I can fix that.