Commenting multi-line command chains?
I often have code where I format by making a long AND/OR statements. For example:
# Get wifi router gateway
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
| cut -d' ' -f5
| tr --delete ;)
Sometimes a single step in a command like above can be complex. Thus I end up wanting to comment it.
For example say the cut
command was more complicated than it really is here. So I want to do something like:
# Get wifi router gateway
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
# Here is a note
| cut -d' ' -f5
| tr --delete ;)
I realize this is invalid syntax.
But I'm curious to see if anyone else has some strategies for commenting long command chains?
bash
add a comment |
I often have code where I format by making a long AND/OR statements. For example:
# Get wifi router gateway
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
| cut -d' ' -f5
| tr --delete ;)
Sometimes a single step in a command like above can be complex. Thus I end up wanting to comment it.
For example say the cut
command was more complicated than it really is here. So I want to do something like:
# Get wifi router gateway
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
# Here is a note
| cut -d' ' -f5
| tr --delete ;)
I realize this is invalid syntax.
But I'm curious to see if anyone else has some strategies for commenting long command chains?
bash
Do you mean "add a comment", or "comment out the line"?
– Jeff Schaller
Nov 21 '17 at 18:34
@JeffSchaller add comment like the part that says# Here is a note
– Philip Kirkbride
Nov 21 '17 at 18:44
add a comment |
I often have code where I format by making a long AND/OR statements. For example:
# Get wifi router gateway
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
| cut -d' ' -f5
| tr --delete ;)
Sometimes a single step in a command like above can be complex. Thus I end up wanting to comment it.
For example say the cut
command was more complicated than it really is here. So I want to do something like:
# Get wifi router gateway
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
# Here is a note
| cut -d' ' -f5
| tr --delete ;)
I realize this is invalid syntax.
But I'm curious to see if anyone else has some strategies for commenting long command chains?
bash
I often have code where I format by making a long AND/OR statements. For example:
# Get wifi router gateway
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
| cut -d' ' -f5
| tr --delete ;)
Sometimes a single step in a command like above can be complex. Thus I end up wanting to comment it.
For example say the cut
command was more complicated than it really is here. So I want to do something like:
# Get wifi router gateway
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
# Here is a note
| cut -d' ' -f5
| tr --delete ;)
I realize this is invalid syntax.
But I'm curious to see if anyone else has some strategies for commenting long command chains?
bash
bash
edited Dec 15 at 16:54
Rui F Ribeiro
38.9k1479129
38.9k1479129
asked Nov 21 '17 at 18:21
Philip Kirkbride
2,3662780
2,3662780
Do you mean "add a comment", or "comment out the line"?
– Jeff Schaller
Nov 21 '17 at 18:34
@JeffSchaller add comment like the part that says# Here is a note
– Philip Kirkbride
Nov 21 '17 at 18:44
add a comment |
Do you mean "add a comment", or "comment out the line"?
– Jeff Schaller
Nov 21 '17 at 18:34
@JeffSchaller add comment like the part that says# Here is a note
– Philip Kirkbride
Nov 21 '17 at 18:44
Do you mean "add a comment", or "comment out the line"?
– Jeff Schaller
Nov 21 '17 at 18:34
Do you mean "add a comment", or "comment out the line"?
– Jeff Schaller
Nov 21 '17 at 18:34
@JeffSchaller add comment like the part that says
# Here is a note
– Philip Kirkbride
Nov 21 '17 at 18:44
@JeffSchaller add comment like the part that says
# Here is a note
– Philip Kirkbride
Nov 21 '17 at 18:44
add a comment |
3 Answers
3
active
oldest
votes
This seems to work in Bash, dash
, etc:
#!/bin/sh
seq 20 | # make a long list
grep '[234]' # mut only take part of it
Similarly with &&
or ||
in place of the pipe, and also inside $( ... )
.
add a comment |
The only thing that makes your command have invalid syntax is the fact that you needlessly escape the newlines on each line.
Your command,
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
| cut -d' ' -f5
| tr --delete ;)
is identical to
gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers | cut -d' ' -f5 | tr --delete ;)
In other words, from the shell's point of view, it's not a multi-line command at all.
Inserting a comment into this before the cut
makes the command substitution unterminated (the final )
is commended out):
gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers # Here is a note | cut -d' ' -f5 | tr --delete ;)
However, there is no need to escape the newlines. The following is totally valid code:
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
| cut -d' ' -f5
| tr --delete ;)
Here, it's just fine to insert a comment:
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
# Here is a note
| cut -d' ' -f5
| tr --delete ;)
It's fine to do this because each part of a pipeline may be followed by one or several newlines, and a comment is ignored by the shell's parser.
Looking at the actual code, this is a safer and more portable variant (except for the initial grep
that still requires using non-standard extensions):
gateway=$(grep -A 5 -m 1 -e "$wifi" <"$leases"
| grep -F 'option routers'
# Here is a note
| cut -d ' ' -f 5
| tr -d ';' )
add a comment |
Also, zsh
, which can emulate bash (just call it as bash or run emulate bash
), actually supports multi-line commands!
Just run bindkey -e
or bindkey "^[^M" self-insert-unmeta
, and then you can go to a new line by hitting M-Return.
It is saved as one history entry; you can move around with the arrow keys as you would expect. It will only switch entries when you go up/down at the top/bottom row.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f406101%2fcommenting-multi-line-command-chains%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This seems to work in Bash, dash
, etc:
#!/bin/sh
seq 20 | # make a long list
grep '[234]' # mut only take part of it
Similarly with &&
or ||
in place of the pipe, and also inside $( ... )
.
add a comment |
This seems to work in Bash, dash
, etc:
#!/bin/sh
seq 20 | # make a long list
grep '[234]' # mut only take part of it
Similarly with &&
or ||
in place of the pipe, and also inside $( ... )
.
add a comment |
This seems to work in Bash, dash
, etc:
#!/bin/sh
seq 20 | # make a long list
grep '[234]' # mut only take part of it
Similarly with &&
or ||
in place of the pipe, and also inside $( ... )
.
This seems to work in Bash, dash
, etc:
#!/bin/sh
seq 20 | # make a long list
grep '[234]' # mut only take part of it
Similarly with &&
or ||
in place of the pipe, and also inside $( ... )
.
edited Nov 21 '17 at 18:39
answered Nov 21 '17 at 18:28
ilkkachu
55.5k783151
55.5k783151
add a comment |
add a comment |
The only thing that makes your command have invalid syntax is the fact that you needlessly escape the newlines on each line.
Your command,
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
| cut -d' ' -f5
| tr --delete ;)
is identical to
gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers | cut -d' ' -f5 | tr --delete ;)
In other words, from the shell's point of view, it's not a multi-line command at all.
Inserting a comment into this before the cut
makes the command substitution unterminated (the final )
is commended out):
gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers # Here is a note | cut -d' ' -f5 | tr --delete ;)
However, there is no need to escape the newlines. The following is totally valid code:
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
| cut -d' ' -f5
| tr --delete ;)
Here, it's just fine to insert a comment:
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
# Here is a note
| cut -d' ' -f5
| tr --delete ;)
It's fine to do this because each part of a pipeline may be followed by one or several newlines, and a comment is ignored by the shell's parser.
Looking at the actual code, this is a safer and more portable variant (except for the initial grep
that still requires using non-standard extensions):
gateway=$(grep -A 5 -m 1 -e "$wifi" <"$leases"
| grep -F 'option routers'
# Here is a note
| cut -d ' ' -f 5
| tr -d ';' )
add a comment |
The only thing that makes your command have invalid syntax is the fact that you needlessly escape the newlines on each line.
Your command,
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
| cut -d' ' -f5
| tr --delete ;)
is identical to
gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers | cut -d' ' -f5 | tr --delete ;)
In other words, from the shell's point of view, it's not a multi-line command at all.
Inserting a comment into this before the cut
makes the command substitution unterminated (the final )
is commended out):
gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers # Here is a note | cut -d' ' -f5 | tr --delete ;)
However, there is no need to escape the newlines. The following is totally valid code:
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
| cut -d' ' -f5
| tr --delete ;)
Here, it's just fine to insert a comment:
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
# Here is a note
| cut -d' ' -f5
| tr --delete ;)
It's fine to do this because each part of a pipeline may be followed by one or several newlines, and a comment is ignored by the shell's parser.
Looking at the actual code, this is a safer and more portable variant (except for the initial grep
that still requires using non-standard extensions):
gateway=$(grep -A 5 -m 1 -e "$wifi" <"$leases"
| grep -F 'option routers'
# Here is a note
| cut -d ' ' -f 5
| tr -d ';' )
add a comment |
The only thing that makes your command have invalid syntax is the fact that you needlessly escape the newlines on each line.
Your command,
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
| cut -d' ' -f5
| tr --delete ;)
is identical to
gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers | cut -d' ' -f5 | tr --delete ;)
In other words, from the shell's point of view, it's not a multi-line command at all.
Inserting a comment into this before the cut
makes the command substitution unterminated (the final )
is commended out):
gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers # Here is a note | cut -d' ' -f5 | tr --delete ;)
However, there is no need to escape the newlines. The following is totally valid code:
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
| cut -d' ' -f5
| tr --delete ;)
Here, it's just fine to insert a comment:
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
# Here is a note
| cut -d' ' -f5
| tr --delete ;)
It's fine to do this because each part of a pipeline may be followed by one or several newlines, and a comment is ignored by the shell's parser.
Looking at the actual code, this is a safer and more portable variant (except for the initial grep
that still requires using non-standard extensions):
gateway=$(grep -A 5 -m 1 -e "$wifi" <"$leases"
| grep -F 'option routers'
# Here is a note
| cut -d ' ' -f 5
| tr -d ';' )
The only thing that makes your command have invalid syntax is the fact that you needlessly escape the newlines on each line.
Your command,
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
| cut -d' ' -f5
| tr --delete ;)
is identical to
gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers | cut -d' ' -f5 | tr --delete ;)
In other words, from the shell's point of view, it's not a multi-line command at all.
Inserting a comment into this before the cut
makes the command substitution unterminated (the final )
is commended out):
gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers # Here is a note | cut -d' ' -f5 | tr --delete ;)
However, there is no need to escape the newlines. The following is totally valid code:
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
| cut -d' ' -f5
| tr --delete ;)
Here, it's just fine to insert a comment:
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
# Here is a note
| cut -d' ' -f5
| tr --delete ;)
It's fine to do this because each part of a pipeline may be followed by one or several newlines, and a comment is ignored by the shell's parser.
Looking at the actual code, this is a safer and more portable variant (except for the initial grep
that still requires using non-standard extensions):
gateway=$(grep -A 5 -m 1 -e "$wifi" <"$leases"
| grep -F 'option routers'
# Here is a note
| cut -d ' ' -f 5
| tr -d ';' )
edited Dec 15 at 23:16
answered Dec 15 at 17:39
Kusalananda
121k16229372
121k16229372
add a comment |
add a comment |
Also, zsh
, which can emulate bash (just call it as bash or run emulate bash
), actually supports multi-line commands!
Just run bindkey -e
or bindkey "^[^M" self-insert-unmeta
, and then you can go to a new line by hitting M-Return.
It is saved as one history entry; you can move around with the arrow keys as you would expect. It will only switch entries when you go up/down at the top/bottom row.
add a comment |
Also, zsh
, which can emulate bash (just call it as bash or run emulate bash
), actually supports multi-line commands!
Just run bindkey -e
or bindkey "^[^M" self-insert-unmeta
, and then you can go to a new line by hitting M-Return.
It is saved as one history entry; you can move around with the arrow keys as you would expect. It will only switch entries when you go up/down at the top/bottom row.
add a comment |
Also, zsh
, which can emulate bash (just call it as bash or run emulate bash
), actually supports multi-line commands!
Just run bindkey -e
or bindkey "^[^M" self-insert-unmeta
, and then you can go to a new line by hitting M-Return.
It is saved as one history entry; you can move around with the arrow keys as you would expect. It will only switch entries when you go up/down at the top/bottom row.
Also, zsh
, which can emulate bash (just call it as bash or run emulate bash
), actually supports multi-line commands!
Just run bindkey -e
or bindkey "^[^M" self-insert-unmeta
, and then you can go to a new line by hitting M-Return.
It is saved as one history entry; you can move around with the arrow keys as you would expect. It will only switch entries when you go up/down at the top/bottom row.
edited Nov 21 '17 at 19:32
answered Nov 21 '17 at 19:00
SilverWolf
1215
1215
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f406101%2fcommenting-multi-line-command-chains%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you mean "add a comment", or "comment out the line"?
– Jeff Schaller
Nov 21 '17 at 18:34
@JeffSchaller add comment like the part that says
# Here is a note
– Philip Kirkbride
Nov 21 '17 at 18:44