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
shopt ...notset...shopt. Thank you for the answer. If you want to explain more, don't hesitate :)