Bash the Hash! – a Symbolic Feature of Bash Comment

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Hashing is the main piece of the solution in a Bash-coded puzzle. The hash (#) symbol put on the initial point of any line resembles an explanatory statement of any complicated program in Bash script. In this article, I will explain the term ‘Bash the hash’ with its practical execution. Let’s explore!

Key Takeaways

  • Learning what is hashing in Bash script.
  • Getting ideas about the execution of Bash hashing.

What is Bash Hashing?

Bash hashing is the symbolic formula for marking out single-line comments & multiple-line comments in Bash script. With some exceptions, putting a hash(#) symbol before any line is enough to call it a Bash comment. Apparently, it makes the compiler bound to skip the hashed line from the script execution. Thus, hashing turns a line into a non-executable code.

Execution of Bash Hashing

In the following section, you’ll get a visual concept of what happens to a hashed line during any script execution. So, move on to the steps below:

Steps to Follow >

➊ Open your Ubuntu Terminal.

➋ To open a script in the nano text editor, write the following command:

nano hash.sh
EXPLANATION
  • nano: A text editor.
  • hash.sh: This is a script that you can name by any of your choices. Here, I have named the script by ‘hash.sh’.
Opening the script in Nano text editor➌Then, write the following script in the nano editor:

Script (hash.sh) >

#!/bin/bash

#echo "This is a comment."

echo "This is not a comment. So, it is printed."
EXPLANATION

Here, #! /bin/bash: ‘#!’, is called shebang. Now, #echo “This is a comment.” indicates a comment as the line starts with a hash symbol. Lastly, echo “This is not a comment. So, it is printed.” dictates a command that will be executed.

Writing inside the script 'hash.sh'➍ Then, press CTRL+S to save the file & press CTRL+X to exit the nano editor & back to the terminal.

➎ Afterward, use the following command to make the script executable:

chmod u+x hash.sh
EXPLANATION
  • chmod: Changes the permission of the files and directories.
  • u+x: It’s an argument that is used to add the executable permission for the user.
  • hash.sh: The file which you want to make executable. Here, I have used a file named ‘hash.sh’.

Adding executable permission to the script➏ Finally, run the script by the following command:

./hash.sh

Running the script to see the outputFrom the above image, you can see that when I ran the script ‘hash.sh’, it displayed only one line which was written inside the echo command & there was no hash symbol before this line on the script. On the contrary, the line that started with a hash symbol did not execute as it refers only to a statement.

Conclusion

Bash hashing is nothing but a commenting-out feature of a Bash script. Though this is not executable, it participates in making the script more readable and accessible.

People Also Ask

Can I comment out any single-line in Bash without using the hash(#) symbol?
No, to comment a single-line out, it is mandatory to use a hash(#) symbol at the beginning of a line in Bash script.

Can I use the hash(#) symbol for any purpose other than commenting out in Bash?
Yes, you can use the hash symbol for other purposes too. For example, when you use the hash symbol in a variable expansion like ‘${#variable}’, it indicates the length of the variable.

Can the hash(#) symbol cause any unexpected issue in Bash scripting?
If the hash symbol within a string is not properly mentioned, then it may cause a syntax error.

Is there any way to comment out in Bash ignoring the hash(#) symbol?
Yes, you can apply multiple-line commenting in Bash scripting to avoid the hash symbol.

Related Articles


<< Go Back to Bash Comments | Bash Scripting Tutorial

Rate this post
Nadiba Rahman

Hello, This is Nadiba Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation with a bachelor’s degree in Electronics & Telecommunication Engineering from Rajshahi University of Engineering & Technology (RUET).I am quite passionate about crafting. I really adore exploring and learning new things which always helps me to think transparently. And this curiosity led me to pursue knowledge about Linux. My goal is to portray Linux-based practical problems and share them with you. Read Full Bio

Leave a Comment