help-bash
[Top][All Lists]
Advanced

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

Re: access the stdin of the parent process in a pipeline


From: Peng Yu
Subject: Re: access the stdin of the parent process in a pipeline
Date: Thu, 26 Mar 2020 12:06:32 -0500

On 3/25/20, Mike Jonkmans <address@hidden> wrote:
> Apart from the redirection as part of a function declaration,
> you can also use compund commands, as Chet already mentioned.
> One hardly see these constructs in system scripts.
> Though they are properly documented in the bash manual.
>
> Sometimes a 'subshell function' may be useful :
>
>       f() ( ... )

The above is a little strange as the `function` keyword does not work
here without the `()` (as in the commented code below). I'd prefer to
use the function keyword without the first `()` as the typical usage
(`()` really doesn't do anything semantically).

#function f (
#       cd ..
#       pwd
#)

f () (
        cd ..
        pwd
)
function g() (
        cd ..
        pwd
)

f
pwd
g
pwd

Alternatively, I could use the following. But since ( ... ) can be
used as a function body, to avoid the first `()` I have to use { ...
}. This seems to be a little suboptimal decision on the shell grammar.

function f {
(
)
}

> E.g. when 'cd's are done in the body, the working directory gets restored.
>
> If you want to abbreviate a long construct, these can be used:
>
>       f() [[ ... ]]
>       f() (( ... ))
>
> And even:
>       
>       f() for ... done
>       f() if ... fi
>       etc.

These are all good to know. I usually just encode whatever body within
{ }. But it could make the code simpler especially for one line code
like these.

-- 
Regards,
Peng



reply via email to

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