LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-03-2019, 02:16 AM   #1
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,899

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
is this a bug in bash?


or how should it really work?
https://github.com/koalaman/shellcheck/issues/1585

based on my knowledge it looks like a bug. What do you think about it?
 
Old 07-03-2019, 03:39 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You mean a bug in shell-check? No, it warns you because the redirection makes the cycle run in a sub-shell, and you canot set the parent shell's variable from the sub-shell.
 
Old 07-03-2019, 03:46 AM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,804

Rep: Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203
The pipe forces the loop block to run in a sub shell.
Variables and settings in a sub shell are not copied back to the main shell.
All shells (bash,ksh,zsh,dash,mksh) agree in that.

More precise: the code parts left and right from a pipe are forced into a sub shell.

The only exception is ksh: it runs the last part in the main shell:
Code:
failed=0
echo "hello" |
while read line; do
  echo "FAILED"
  failed=1
done
echo "Failed: $failed"
Only ksh says:
Code:
Failed: 1
BTW this proves that mksh, despite its name, is more a Posix shell than a ksh.

Last edited by MadeInGermany; 07-03-2019 at 04:30 AM.
 
Old 07-03-2019, 04:06 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,899

Original Poster
Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
looks like you misunderstood.
The original example does not contain pipe before the while loop.
Code:
#!/bin/bash

echo $BASHPID
failed=0
while read -r _; do
  echo "FAILED"
  echo $BASHPID
  failed=1
done < <(echo hello) | sort -u
echo "Failed: $failed"
And it looks like the while loop still runs in another shell
 
Old 07-03-2019, 04:37 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,804

Rep: Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203
The original example has the loop block left from the pipe. That is always in a sub shell.
I tried to say that in my first sentence.
 
Old 07-03-2019, 06:01 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,899

Original Poster
Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
Yes you are right, I know about that. But this is a bit different case.
 
Old 07-03-2019, 07:58 AM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hello OP,
Since you have not marked the thread as solved I am not sure if your question is answered to your satisfaction. If you are still looking for an explanation then here is the excerpt from the manpage that explains the behaviour:
Code:
   Pipelines
       A  pipeline  is  a sequence of one or more commands separated by one of the control operators | or |&.
       The format for a pipeline is:

              [time [-p]] [ ! ] command [ [|⎪|&] command2 ... ]

       ...

       Each command in a pipeline is executed as a separate process (i.e., in a subshell).
If you are looking for a workaround then process substitution might help:
Code:
#!/usr/bin/bash

failed=0

# process substitution sets failed correctly and sorts
while read -r l; do
        echo $l
        failed=1
done < <(echo "world";echo "hello") > >(sort -u)
echo "Failed: $failed"
Keep in mind though, that 'sort' may output to the screen after the last 'echo'.
Of course, you could also use temporary files to store the value.
 
Old 07-03-2019, 08:19 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,899

Original Poster
Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
Ok, let's have a simple example:
Code:
user@host:/tmp$ echo $$
1984
user@host:/tmp$ echo $$ | sort
1984
Code:
user@host:/tmp$ echo $BASHPID
1984
user@host:/tmp$ echo $BASHPID | sort
27658
based on this (and the man page of bash) this echo is running in a subshell which does not require bash to be reinitialized, and the PIDs ($$) are the same.

something learned again
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Firms bash Bash bug with new round of Shellshock patches LXer Syndicated Linux News 0 09-28-2014 03:51 PM
Bash "shellshock" CVE-2014-6271 CVE-2014-7169 - Bash Bug Fix enorbet Slackware 42 09-27-2014 12:44 AM
Firefox bug? Fedora 7 bug? or what? hawke Fedora 8 07-22-2007 07:00 PM
BUG: soft lockup detected on CPU#0 and BUG: spinlock recursion on CPU#0 ... BloodyCat Linux - Hardware 3 11-07-2006 01:14 PM
Free86 bug or nVidia bug?? ProtoformX Linux - Software 2 05-12-2004 02:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration