ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
No, I don't believe that's possible. the backslash is there to escape the non-printing newline character that normally terminates the line. There can be nothing else following it.
To the shell, the multiple lines appear to be a single line, so the comments can only come after the last one.
Last edited by David the H.; 01-12-2012 at 10:21 PM.
Reason: minor rewording
If you put the pipe at the end of the line Bash will continue with the next line. No need for the backslash.
Code:
$ cat a
#! /bin/bash
echo "one two three" | #comment
cut -d ' ' -f 2
$ ./a
two
$
The comment following the pipe doesn't seem to interfere with the continuation.
I failed to find any support for this in the GNU Bash Reference Manual though, so caveat emptor.
If you put the pipe at the end of the line Bash will continue with the next line. No need for the backslash.
The same also applies for the && and || operators. It also works in dash and tcsh shells too, so I do believe this is common behaviour to all shells.
Actually, it seems to be obliquely defined: all these operators require both sides to exist -- an empty command on either side makes absolutely no sense for these, and a comment or newline or empty line(s) do not produce any statement. The situation is complementary to 'do' or 'then' statements -- they need a preceding semicolon or a new line in Bash, Bourne shells and derivatives and POSIX shells -- so this behaviour is quite intuitive and useful.
I just wish it was explicitly documented somewhere.
Last edited by Nominal Animal; 01-13-2012 at 03:15 AM.
I like your idea but was unable to apply it. Please see if you can modify this contrived code snippet and make it work.
Code:
cat $InFile \ # Read input file
|rev \ # Reverse (flip each line end-for-end)
|rev \ # Reverse, again
> $Work3 # Write interim result to a work file
Daniel B. Martin
What did you try? What happened when you tried it? What error message, if any?
Code:
test$ ls -gGh
total 8.0K
-rwxr--r-- 1 243 2012-01-13 11:02 contrived
-rw-r--r-- 1 24 2012-01-13 10:45 file.in
test$ cat contrived
#! /bin/bash
InFile="file.in"
Work3="file.out"
{
cat $InFile | # Read input file
rev | # Reverse (flip each line end-for-end)
rev # Reverse, again
} > $Work3 # Write interim result to a work file
test$ cat file.in
one
two
three
four
five
test$ ./contrived
test$ ls -gGh
total 12K
-rwxr--r-- 1 243 2012-01-13 11:02 contrived
-rw-r--r-- 1 24 2012-01-13 10:45 file.in
-rw-r--r-- 1 24 2012-01-13 11:04 file.out
test$ cat file.out
one
two
three
four
five
test$
If this style of code is tricky to remember and write then it would be a mistake to adopt it.
I don't see the point of so many comments. Just make a comment on what the pipe accomplishes. For example:
Code:
# cut off last 6 fields
cat $InFile | rev | cut -d, -f7- | rev > $Work3
Let me remind you that this is a contrived example. My real pipes may be much longer. As a matter of personal style I like to comment every line. You might have a different style, and I respect that.
When writing a complicated piece of code (regardless of language) I like to express in words the logic I want to implement. Then, one piece at a time, I fill in the code. When done, my code is fully commented because I started with all comments.
I myself like to use subshells when working with long pipes, i.e.
Code:
(
# First part of the pipe ...
) | (
# Second part of the pipe ...
) | (
# and so on ...
)
For me, a very typical one is something like
Code:
( echo 'plot "datafile" u 1:2 t "data" w points, \'
echo ' cos(x)*exp(x/2.5) t "expected" w lines'
echo "press Enter to close the Gnuplot window" >&2
while read LINE ; do
[ -z "$LINE" ] && break
echo "$LINE"
done
) | gnuplot
which uses Gnuplot to plot some data, but stays interactive. An empty line will close Gnuplot and exit the compound command, but if I happen to think of an additional Gnuplot command -- say set logscale x; replot -- all I need is to type it and hit enter.
On embedded machines with a limited memory subsystem subshells may not be the best option, but for standard Intel/PowerPC et al. architectures, the subshell is forked from the parent using copy-on-write, using physically the same RAM for code and initial data structures, and therefore uses very little actual system resources. (Just a per-process kernel structure for each subshell, I believe.) This means that on a typical workstation or a server, there is no practical difference in resource use between plain pipe commands and piped subshells.
On embedded machines with a limited memory subsystem subshells may not be the best option, but for standard Intel/PowerPC et al. architectures, the subshell is forked from the parent using copy-on-write, using physically the same RAM for code and initial data structures, and therefore uses very little actual system resources. (Just a per-process kernel structure for each subshell, I believe.) This means that on a typical workstation or a server, there is no practical difference in resource use between plain pipe commands and piped subshells.
Neat idea, and well reasoned, but I don't see the advantage over { command-list; }.
Neat idea, and well reasoned, but I don't see the advantage over { command-list; }.
You're absolutely right.
I've just never really bothered to find out about the side effects when using command lists in a pipe (specifically, does shell state propagate or not, or if it is just inherited from the parent shell like subshells) -- and to be honest, I tend to always forget the required semicolon from the end of the command list. I've gravitated to using subshells, because I've felt them to be more intuitive.
To those that are unaware of the semicolon detail with command lists, the equivalent command list variant of piped subshells,
Code:
( echo foo ) | ( cat ; echo bar )
is
Code:
{ echo foo ; } | { cat ; echo bar ; }
Using command lists, the shell does not create unnecessary extra processes. Note the semicolons. If you try
Code:
{ echo foo } | { cat ; echo bar }
the shell does not recognize the braced expressions as command lists, and at least Bash 4.2.10 complains about a syntax error. It's very difficult to realize that the only problem is missing semicolons before closing braces. (Well, unless you remember that the command list syntax is, like Telengard stated above, {command(s)...;} and not just {command(s)...} .)
Last edited by Nominal Animal; 01-14-2012 at 02:42 AM.
I've gravitated to using subshells, because I've felt them to be more intuitive.
That makes sense as an advantage. Parenthesized command groups have simpler syntax and may be easier to type. Curly braces need spaces and the list must end with a control operator.
I have no problem with command grouping. They're basically just an anonymous functions. And it only takes getting caught by the final semicolon thing a few times before you learn to watch out for it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.