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 <[email protected]> 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
>
>
--
[email protected] http://humbleville.blogspot.com