Timeline for Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?
Current License: CC BY-SA 4.0
11 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Apr 4, 2024 at 20:44 | history | edited | Gilles 'SO- stop being evil' | CC BY-SA 4.0 | modern bash can have ksh-like pipe behavior (thanks s3c) |
| Apr 4, 2024 at 20:20 | comment | added | s3c | For bash (Git Bash on Windows 11 at least), to enable last pipeline item in parent shell use set +m (ensuring job control is not active) and shopt -s lastpipe (enabling the actual option). Bash must be version 4.2 or newer. | |
| Apr 26, 2020 at 11:46 | comment | added | Gilles 'SO- stop being evil' | @MikkoRantalainen Beware that for item in $(…) doesn't do the same thing. It expands wildcards unless you disable that with set -f. It splits at all whitespace, not just newlines, unless you change IFS. It strips trailing blank lines. Don't use for item in $(…) unless you've thought of all of these things. | |
| Apr 26, 2020 at 11:44 | comment | added | Mikko Rantalainen | Unfortunately, the syntax for item in $(...) syntax does not nicely support all features. For example ... | while read col1 col2 col3 rest cannot be nicely implemented with for .. in syntax. | |
| Apr 26, 2020 at 11:43 | comment | added | Mikko Rantalainen | Syntax ... | while read item pipes the stdout of whatever is run before while but with this syntax the body of while is always run in a subshell. I prefer to use syntax for item in $(...) instead, because that keeps the body of the loop in current shell. | |
| Apr 13, 2017 at 12:36 | history | edited | CommunityBot | replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/ | |
| Sep 14, 2016 at 18:56 | history | edited | Gilles 'SO- stop being evil' | CC BY-SA 3.0 | added 5 characters in body |
| Sep 14, 2016 at 18:52 | history | edited | GAD3R | CC BY-SA 3.0 | added 2 characters in body |
| Aug 1, 2014 at 21:04 | comment | added | Aquarius Power | it feels like I piped into a function, so I moved some variables and tests to inside it and it worked great, thx! | |
| Mar 24, 2011 at 12:31 | comment | added | Peter.O | Thanks Gilles .. That a=0; a=1 | a=2 gives a very clear picture.. and not only of the localization of internal state, but also that a pipeline doesn't actually need to send anything through the pipe (other than the exit code(?).. In itself that is an interesting insight into a pipe ... I did manage to get my script running with < <(locate -ber ^\.tag$), thanks to the original slightly unclear answer and geekosaur and glenn jackman's comemnts.. I was initially in a dilemma about accepting the answer, but the nett result was pretty clear, especially with jsbillings follow-up comment :) | |
| Mar 23, 2011 at 21:21 | history | answered | Gilles 'SO- stop being evil' | CC BY-SA 2.5 |