4 ms·
I recently had to use eval to solve a shell scripting problem. During the same task, I also discovered that a for loop on it's own is run inside the same shell
by jeffrallen 5y ago
I recently had to use eval to solve a shell scripting problem.
During the same task, I also discovered that a for loop on it's own is run inside the same shell as the code around it (duh), but a for loop which is feeding a pipe is run in a subshell. This was a surprise, because I wanted to set some variables in the for loop, piping progress indications to the `dialog` program, and then use those variables afterwards. My variables kept disappearing after the for loop exited, but not when I removed the progress indications.
Shell sucks.
- aaaaaaaaaaab 5y agoHehe. Rookie mistake. This is how you do it: i=0 for foo in …; do i=((i + 1)) done > >(stuff | goes | here)
- _0w8t 5y agoThis uses Bash extension not available in POSIX. Bash also has lastpipe shopt that helps when one needs to read from a process. To stay compatible with POSIX one needs to use either named pipes or extra file discriptors to communicate from the sub shell.
- aaaaaaaaaaab 5y agoYeah, but in my experience limiting yourself to the POSIX sh feature set is almost always just nitpicking. Bash is ubiquitous enough that this is hardly a problem in practice.
- _0w8t 5y agoIn my opinion the moment POSIX shell is not enough, one better use more appropriate language than learning all Bash-specific syntax rules and exceptions. And with containers bash may not be even there.
- aaaaaaaaaaab 5y agoWhich language would be better than bash for building concurrent pipelines from other utilities? Sure, you can build pipelines in Python via the subprocess module, but it’s not exactly an ergonomic API…