[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Is there a way to get the output of a function without an extra bash
From: |
Eli Schwartz |
Subject: |
Re: Is there a way to get the output of a function without an extra bash process? |
Date: |
Sun, 10 May 2020 17:34:32 -0400 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 |
On 5/10/20 5:20 PM, Peng Yu wrote:
> Hi,
>
> $(fun) by default will always use a new bash process. This is wasteful
> in certain cases. In there a way to use the existing bash process
> (without using a temp file)? Thanks.
Yes, of course there is.
> $ cat ./main2.sh
> #!/usr/bin/env bash
> # vim: set noexpandtab tabstop=2:
>
> set -v
> echo "$BASHPID"
> function f {
> echo "$BASHPID"
> }
> echo "$(f)"
> $ ./main2.sh
> echo "$BASHPID"
> 32056
> function f {
> echo "$BASHPID"
> }
> echo "$(f)"
> 32057
Have you tried this:
$ cat ./main2.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:
set -v
echo "$BASHPID"
function f {
echo "$BASHPID"
}
f
$
And for actual, real-world cases like the one you actually care about
instead of the nonsense example you provided here which doesn't actually
describe your use cases...
You can have the function write to global variables. This may be
considered undesirable clobbering of global state, but you can certainly
do it.
In order to run external disk executables you are not going to escape
the need to fork+exec the external disk executable (unless you want to
write a loadable builtin I suppose), and external disk executables don't
get to write to the internal memory of the bash process. (There are
loadable builtins for that, in which case you are no longer in the land
of external disk *executables*.)
So whatever you're doing to save the output of those, you need to do
anyway, but there's nothing forcing you to use contrived cases like
literally `echo "$(func)"` to reuse the output of that function.
--
Eli Schwartz
Arch Linux Bug Wrangler and Trusted User
signature.asc
Description: OpenPGP digital signature
- Is there a way to get the output of a function without an extra bash process?, Peng Yu, 2020/05/10
- Re: Is there a way to get the output of a function without an extra bash process?,
Eli Schwartz <=
- Re: Is there a way to get the output of a function without an extra bash process?, Peng Yu, 2020/05/10
- Re: Is there a way to get the output of a function without an extra bash process?, Chet Ramey, 2020/05/11
- Re: Is there a way to get the output of a function without an extra bash process?, Koichi Murase, 2020/05/11
- Re: Is there a way to get the output of a function without an extra bash process?, Peng Yu, 2020/05/11
- Re: Is there a way to get the output of a function without an extra bash process?, Chet Ramey, 2020/05/12
Re: Is there a way to get the output of a function without an extra bash process?, Chris F.A. Johnson, 2020/05/10