bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: The correct way to use "for" without polluting the environment


From: Alan Wild
Subject: Re: The correct way to use "for" without polluting the environment
Date: Sat, 7 Mar 2015 16:46:52 -0600

I'm really curious to see if anyone else offers better ideas, but the ways I've done this are

1) exactly what you propose.  

2) use a subshell (parantheses):

$ ( for x in a b c; { echo $x; } )
a
b
c
$ typeset -p x
bash: typeset: x: not found

3) use a function and declare x local to the function

$ function f() { local x; for x; { echo "$x"; }; }
$ f a b c
a
b
c
$ typeset -p x 
bash: typeset: x: not found

-Alan


On Sat, Mar 7, 2015 at 4:31 PM, Peng Yu <address@hidden> wrote:
Hi, I use unset to remove x from the environment once the for loop is
finished. Is it the best way to do in bash? Thanks.

for x in a b c
do
  echo "$x"
done
unset x

--
Regards,
Peng




--
address@hidden http://humbleville.blogspot.com

reply via email to

[Prev in Thread] Current Thread [Next in Thread]