1828745 Members
2748 Online
109985 Solutions
New Discussion

variables in bash

 
Alex Lavrov
Regular Advisor

variables in bash

hey,

I'm using bash in r.h. AS3.0 .

in the script I declare:
VAR=5

then I run a while loop that adds data to the variable, but after the loop the variable returns to be 5.

how can I make it save the changes I have done in the loop. in sh in HP-UX11i it works, but here it doesn't work.

thanx,
15 REPLIES 15
Alexander Chuzhoy
Honored Contributor

Re: variables in bash

how about export?
like export


to add a variable to a shell (across logouts):

add a line to /etc/bashrc (for all users)

or

to ~/.bashrc for a specific user


then do logout and login..


Alex Lavrov
Regular Advisor

Re: variables in bash

You mean to export every variable?
A bit ugly, isn't it?

These scripts work fine in the bash of HPUX, what's the difference? Some flag maybe?
Martin P.J. Zinser
Honored Contributor

Re: variables in bash

Hello Alex,

have a look at the examples in

http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html#ss7.1

Works for me on Linux. If you still have problems with the script after reading this please do post a >>short<< reproducer here.

Greetings, Martin
Alexander Chuzhoy
Honored Contributor

Re: variables in bash

Sorry for the delay!
Can you post the troublemaking part of your script?
Mark Grant
Honored Contributor

Re: variables in bash

You won't need to export your variable if you are just using in a loop in the script.

I would assume that you have a slight loginc problem somewhere, could you post the relevant part of the script.
Never preceed any demonstration with anything more predictive than "watch this"
Alex Lavrov
Regular Advisor

Re: variables in bash

I think I didn't make myself clear,
here the script:

x=0
v="hello"
cat /etc/fstab | while read line
do
x=`echo $x+1 | bc`
v=$v$line echo $v
done
echo $v
something like this, the output I get:
helloline1
helloline1line2
helloline1line2line3
.
.
.
hello

as u can see, outside the loop, "v" stayed unchanged ...
Mark Grant
Honored Contributor

Re: variables in bash

I don't pretend to understand why this happens but you can achieve what you want with the following loop and it works.

IFS="
"
x=0;
v="hello"
for line in `cat /etc/fstab`
do
(( x = x + 1 ))
v=$v$line
echo "$v"
done
echo "LAST ONE $v"
Never preceed any demonstration with anything more predictive than "watch this"
Alex Lavrov
Regular Advisor

Re: variables in bash

well, probably it happens because of the pipe ... but in HPUX11.11 it works fine.
Mark Grant
Honored Contributor

Re: variables in bash

This look like some kind of bug with bash. I tried it using bash on HPUX and it still fails. I tried it using /bin/sh and it works fine.

If behaves as if the "cat /etc/fstab | while read line" is creating a subshell which it may well do. However, if this is the case, then increasing the value of "x" at initialization would have no effect but it does. Also, exporting "v" would solve the problem but it doesn't.

On the other hand, I've never really trusted piping into my own script so maybe I'll stick with that distrust :)
Never preceed any demonstration with anything more predictive than "watch this"
Stuart Browne
Honored Contributor

Re: variables in bash

Nup, not a bug. We discussed this recently in the thread http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=478229 here.

Basically, any commands executed in pipe are executed as a SubShell, thus environment variables changed within the pipe (read: subshell) stay there.
One long-haired git at your service...
Mark Grant
Honored Contributor

Re: variables in bash

Stuart,

This is how it looked to me too BUT if it really is being run in a sub-shell, you would expect that setting $x to say 10 and not exporting it would have no effect on the loop in the pipe. However, it does. *shrug*

I would be interested to know what is "correct". Personally I think that "bash" is wrong here because the posix complient shell on hpux does not have this behaviour. It works as you would expect.
Never preceed any demonstration with anything more predictive than "watch this"
Stuart Browne
Honored Contributor

Re: variables in bash

I'm just going by what it says in the man page ;)
One long-haired git at your service...
Alex Lavrov
Regular Advisor

Re: variables in bash

But how it works on HPUX?
I'm runnig sh on both of them, HPUX and linux.
Stuart Browne
Honored Contributor

Re: variables in bash

Umm, in your original post, you said 'bash in r.h. AS3.0'.

It won't increment it within a piped loop on Linux (using bash), or using pdksh.

As for other shells, I can't guarantee.

sh/ksh on HPUX is a different story.
One long-haired git at your service...
Stuart Browne
Honored Contributor

Re: variables in bash

.. just for the sake of experiment ..

your routine there could do what you expect it if you not use a pipe, i.e.

while read line
do

done < /etc/fstab
One long-haired git at your service...