Re: [Help-bash] Is there a construct in bash that can replace cat?
From:
Greg Wooledge
Subject:
Re: [Help-bash] Is there a construct in bash that can replace cat?
Date:
Tue, 20 Jan 2015 08:10:52 -0500
User-agent:
Mutt/1.4.2.3i
On Mon, Jan 19, 2015 at 05:03:41PM -0600, Peng Yu wrote:
> Is there a way to do it in
> bash internally without resorting to an external program?
> function filter {
> if [ "$totext" ]
> then
> col -b
> else
> cat
> fi
> }
>
> some_program | filter
You're already forking because of the pipeline, so having the child shell
execute cat isn't much of an additional slowdown.
That said, you could rewrite it something like this:
# Arguments: a command to run, plus its arguments
# Whether the command is actually filtered depends on the global variable
# totext.
filter() {
if [ "$totext" ]; then
"$@" | col -b
else
"$@"
fi
}
filter some_program arg1 'arg 2'
Of course this has the drawback that "some_program arg1 'arg 2' ..."
cannot be a pipeline or compound command. But that may be acceptable
in your application.