3 ms·
Yeah, although I use the parentheses mostly because I like how it reads. And that process substitution trick is important too. I think the redirection can come
by fiddlerwoaroof 5y ago
Yeah, although I use the parentheses mostly because I like how it reads. And that process substitution trick is important too.
I think the redirection can come first, though (not at a computer to test):
< <( do_something ) while read . . .
- aaaaaaaaaaab 5y agoYeah, for commands, the input/output redirections can precede them, but for some reason it doesn't work for builtin constructs like `while`: $ < <( echo foo ) while read -r f; do echo "$f"; done -bash: syntax error near unexpected token `do' $ < <( echo foo ) xargs echo foo $ bash --version GNU bash, version 5.1.4(1)-release (x86_64-apple-darwin20.2.0)
- fiddlerwoaroof 5y agoMaybe wrap the loop either with parentheses or braces?
- aaaaaaaaaaab 5y agoTried that, but nope :D I'll let you figure this one out once you get near a computer!
- entire-name 5y agoRedirection like this doesn't seem to work if it comes first on GNU bash 5.0.17(1)-release. For documentation purposes, this is the exact thing I tried to run: $ < <(echo hi) while read a; do echo "got $a"; done -bash: syntax error near unexpected token `do' $ while read a; do echo "got $a"; done < <(echo hi) got hi Maybe there is another way...
- JNRowe 5y agoOne way which isn't great, but an option nonetheless… The zsh parser is happy with that form: $ zsh -c '< <(echo hi) while read a; do echo "got $a"; done' got hi My position isn't that it is a good reason to switch shells, but if you're using it anyway then it is an option.
- fiddlerwoaroof 5y agoI’ve always preferred zsh and, as I’ve slowly adopted nix, I’ve slowly stopped writing bash in favor of zsh
- deleted 5y ago[deleted]
- lottin 5y agoThis is not POSIX compliant though.
- fiddlerwoaroof 5y agoThese days bash and/or zsh are available nearly every place I care about, so I find POSIX compliance to be much less relevant.
- pgtan 5y agoNo, process substitution must be provided by the kernel/syslibs, it is not feature of bash. For example there is bash on AIX, but process substitution is not possible because the OS do not support it.
- nonesuchluck 5y agoksh93 depends exclusively on the kernel implementation of /dev/fd devices. I just checked `cat <(ls)` a moment ago on both Linux and AIX 7.2--the latter fails in ksh93t+. Bash uses /dev/fd when available, but also appears to have an internal implementation which silently creates named pipes and cleans them up. In Bash 5.0.18 on AIX, fake process substitution works just fine, in my testing.
- pgtan 5y agoYes, you are right. Bash 5 on AIX 7.2 works with process substitution. Thanks for the advise!