Solved Comment out a block of code in a shell script

I asked ChatGPT the question in the title and was provided with:-

sh:
: <<'COMMENT'
echo "This won't run"
rm -rf /tmp/*
echo "Neither will this"
COMMENT

I have never come across this. Does is look correct?
 
I use :: on Windows but not sure about 1 colon; haven't tried or heard of that FreeBSD or Linux (bash and sh I use #)

Nah I forgot I used that a little while ago :p (had this result in history)

When running scripts from desktop launchers on GNOME, I'd use zenity to report a message when it's done. ptyxis closes on command complete, but kgx stays open; kgx staying open would report the zenity command failed at the end (error 1280?) leaving a red bar at the bottom. Putting a colon at the end fixed that 😅 (the end # didn't stop the red error)

Code:
rm -f ~/'Downloads/IW4x-release-latest.zip'

zenity --title 'IW4x Updater' --text 'Client and Rawfiles update complete' --icon ~/'.wine/Icons/IW4x-256.png' --timeout '2' --info
:

# End

Didn't try using : as a multi-line comment though, but there's no way I'd try it like that with a rm -rf :p
 
You are essentially marking a block of text as "COMMENT" and with a small change you could feed that block of text to something for processing...but what you've found is indeed a nifty way to mark a block as a comment without "#" on every line.

My preference is to mark each individual comment line with "#" because it is more obvious when scanning thru the code. While your way works, do be careful as it could lead to obscure bugs if you aren't paying attention.
 
While this is valid to suppress output it is not a good way to do this.

For example, backticks get executed. Consider this:
Code:
: << EOF
echo hello
echo `touch /tmp/l88`
EOF

This will execute the touch.
The OP had the delimiter (COMMENT, in their case) quoted on the initiating line, which would not run the back-ticked expansion:

sh:
$ : <<'EOF'
echo hello
echo `touch /tmp/l88`
EOF
$ ls -l /tmp/l88
ls: /tmp/l88: No such file or directory

That said, this likely isn’t highlighted as a comment by most editors, so I think I would tend towards vanilla comments.
 
A non-referencing here document? That's no universal shell code.
It also isn't a valid question. That's asking for hallucination.

Still looking for a nice way to put them in formatted bash scripts without requiring the 0-indentation of the EOF word. I only know an ugly cut variant.
 
: is a holdover from Thompson shell where it used to be used for implementing labels for goto(1).
It was a command that did nothing but return true and simply discarded its arguments.
goto(1) would scan stdin forward until it found a line that started with its argument followed by a colon. It skips the rest of the line and returns to the shell with its file pointer advanced.
This label/comment trick was also used in MS-DOS.
 
: is a holdover from Thompson shell where it used to be used for implementing labels for goto(1).
It was a command that did nothing but return true and simply discarded its arguments.
goto(1) would scan stdin forward until it found a line that started with its argument followed by a colon. It skips the rest of the line and returns to the shell with its file pointer advanced.
This label/comment trick was also used in MS-DOS.
This was anti-user behavior. MS-DOS was intentionally real bad at CLI operation. It was pretty much impossible to make an interactive batch script that doesn't bloat up the screen and accepts normal keyboard input.
Better compile it in C or so, but that again had the problem of needing to load an entire command.com sub-session to execute a command from within a exe. Like processes can't communicate directly in a single-thread system...
I believe they were already fighting UNIX-likes where the entire systen is within reach of the operator for free because it was a threat to their binary castle where software components are sold as physical features for the easy profit.
 
Back
Top