1

I am wondering if it is possible to turn off the comment feature in noninteractive bash script.

Turning on or off "interactive_comments" or "interactive-comments" with set or shopt has no effect on the non-interactive shell.

Why did this happen? What is the point I'm missing?

5
  • Did you misinterpret "interactive_comments" (typo of dash for underscore) for "interactive"?
    – Jeff Schaller
    Jul 10, 2020 at 17:17
  • ... because it's actually shopt ... not set...
    – Jeff Schaller
    Jul 10, 2020 at 17:20
  • @JeffSchaller Thank you very much, I will investigate shopt. Thank you for the answer. If you want to explain more, don't hesitate :)
    – testter
    Jul 10, 2020 at 17:21
  • I'm just trying to get to the core issue, and typo-fixes aren't terribly interesting. Please feel free to edit your Question to focus on what you're most confused by.
    – Jeff Schaller
    Jul 10, 2020 at 17:22
  • @JeffSchaller In fact, all I wonder is how to use the "interactive-comments" option on bash. I was just expecting to get an interactive shell using this(interactive-comments) option.
    – testter
    Jul 10, 2020 at 17:24

1 Answer 1

4

I was just expecting to get an interactive shell using this(interactive-comments) option.

Read the documentation instead of guessing.

If set, allow a word beginning with # to cause that word and all remaining characters on that line to be ignored in an interactive shell

Turning on the option doesn't make the shell interactive. It controls whether # starts a comment if the shell is interactive, and has no effect if the shell isn't interactive.

Also,

This option is enabled by default.

So turning it on has no effect.

Also, the documented way is shopt -s interactive_comments to turn it on and shopt -u interactive_comments to turn it off. But set -o interactive-comments and set +o interactive-comments are (undocumented, I think) alternatives.


You can only get an interactive shell if you start it as an interactive shell. There's no way to make a non-interactive shell interactive. Whether the shell is interactive or not depends on its command line and on whether it's running on a terminal:

  • With the option -i, bash starts interactively.
  • Without -i but with -c or with a script file name (bash -c 'some command' or bash path/to/script), bash starts non-interactively.
  • Without -i, -c or a script file name, bash starts interactively if its standard input and standard error are both terminals, and non-interactively otherwise.

Regardless of the status of the interactive_comments option, comments in script files and in -c scripts are always recognized. The only case in which # at the start of a word doesn't start a comment is if the shell is interactive, the interactive_comments option has been turned off, and it's reading from standard input.

Try this script.sh:

# a comment
shopt interactive_comments
set +o interactive-comments
shopt interactive_comments
# another comment

Execution transcript:

bash-5.0$ # a comment
bash-5.0$ shopt interactive_comments
interactive_comments    on
bash-5.0$ set +o interactive-comments
bash-5.0$ shopt interactive_comments
interactive_comments    off
bash-5.0$ # another comment
bash: #: command not found
bash-5.0$ exit
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .