Index
Subject
: Re: LUG: seeking elegant solution
From
: Kevin Hunter <hunteke@earlham.[redacted]>
Date
: Thu, 17 Nov 2011 09:49:43 -0500
Parent
At 8:31pm -0500 Wed, 16 Nov 2011, Stephen Roller wrote:
> Slight modification from Alex's post:
>
> for file in $(ls --color=none 2/); do cp 1/$file 2/$file; done
>
> The --color=none might not be necessary, but it was for me. The $ in
> front of the file inside the loop is definitely necessary.
The need for --color=none stems from either an alias or an environment
variable. In this context, alias is a way of telling the shell "when I
type this, I really mean /that/". Try typing 'alias ls' at your prompt
to see how your alias is set. Since you received colored output in that
construct, I suspect your alias includes --color=always, which ignores
where the output is going.
Another solution might have been to ignore the alias with the
introduction of a backslash:
$ for f in $(\ls 2/); do ...
And though it doesn't play as nice with other command line tools, (e.g.
less), you could also update your alias to use =auto instead:
$ alias ls='/bin/ls --color=auto'
This will make ls only output color if it's talking directly to the
terminal. If it's talking to a pipe, or a subshell, it will not. (For
folks looking to programatically check this, isatty() is the function call.)
Cheers,
Kevin