'print bash script comments when bash is executed

Assuming I have a bash script test.sh:

#!/bin/bash

# Comment 1
echo "Hello world"

# Comment 2
echo "Hi there"

I know after making the bash script executable and running it, only the echo commands would show as output.

I'm wondering if there's a way to print the comments on execution. That is, I want the output to look like:

Comment 1
Hello world

Comment 2
Hi there


Solution 1:[1]

if there's a way to print the comments on execution

No, there is no way.

a way

Modify bash source and modify langauge parser to parse # characters to work as ; echo.

In trivially simple cases that do not need a real blown parser, you could replace all # in the source file by echo and run it then. Like sed 's/#/echo/' test.sh | bash -s.

Solution 2:[2]

Like what user1934428 said

Not exactly what you want but if you set the shebang to #!/bin/sh -x (or -v) it will print out both the comments and the commands in the script

Additional Reference: https://stackoverflow.com/a/2853811/10359765

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KamilCuk
Solution 2 evantkchong