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 <pengyu...@gmail.com> 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
>
>


-- 
a...@madllama.net http://humbleville.blogspot.com

Reply via email to