bash subshells have trouble with && or || on next line
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
bash subshells have trouble with && or || on next line
I have a problem with bash/sh scripts that use subshells. Two sample shell scripts below, both with nested subshells, the first works and the second doesn't. The difference is that the logical 'and' (&&) is on the end of line one in the first but on the beginning of line two in the second faulty script. There is no whitespace after the '\' delimiter. The scripts execute just fine on a Solaris 2.8 box. Platform is RH ES 3.0 2.4.21-20.0.1, bash version is GNU bash, version 2.05b.0(1)-release (i386-redhat-linux-gnu). Is this a bug or expected behaviour? Any advice anyone out there can offer would be appreciated. Thx.
# cat /tmp/test1
((echo line1) && \
(echo line2))
# head -1 /tmp/test1 | od -c
0000000 ( ( e c h o l i n e 1 ) & &
0000020 \ \n
0000023
#
# cat /tmp/test2
((echo line1) \
&& (echo line2))
# head -1 /tmp/test2 | od -c
0000000 ( ( e c h o l i n e 1 ) \ \n
0000020
#
# bash /tmp/test1
line1
line2
#
# bash /tmp/test2
/tmp/test2: line 2: syntax error near unexpected token `'
/tmp/test2: line 2: `&& (echo line2))'
Take a look at the parentheses: one unmatch open paren on the first line; one unmatched closes paren on the second. Try removing the unmatched paren from each line.
There are easier fixes than removing the unmatched parens. But I'm not after a fix, I'm trying to confirm if this is expected behaviour or a bug. IMHO, the '\' should tell bash the next line is a continuation of the previous line, so bash shouldn't report unmatched parens error. This is how it behaves under Solaris. The issue is not to find a workaround, we have lots. The question is whether or not this is a bug that the Linux community and Red Hat should fix or is it expected behaviour.
That's not the problem. The problem arises when running within a script. You essentially eliminated the problem by putting both subshells on the same line, which is not what we're after. I have all the fixes we need. We need to know if this should be fixed in bash. Need some high-level shell gurus to step in here.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.