2

What are the uses of the hash character in shell scripting beyond using them in comments or as $#; what are other uses?

I want to be able to grep the content on each line before hash since there are comments in the same line as code.

The application context is mainly korn shell, the system I use is "HP-UX ***B.11.31 U ia64 *****unlimited-user license".

closed as unclear what you're asking by Gilles, Anthon, slm Mar 14 '15 at 13:25

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    You're asking for all of the uses of the hash symbol # in bash scripting? – iyrin Mar 14 '15 at 10:40
  • mainly korn shell – anotherperson1 Mar 14 '15 at 11:42
  • 1
    What are you actually wanting to do? Search through code while ignoring comments? – roaima Mar 14 '15 at 11:48
  • 1
    Unfortunately, you can't find comments simply by using grep because of the complexity of the shell's token recognition. A # that occurs after a backslash, or in a string, or as part of a shell parameter like $# or ${parameter#word}, or (in some shells) numeric constants like base#number, is not treated as a comment. – Mark Plotnick Mar 14 '15 at 11:49
  • OP:yes @roaima that is what I want (also mentioned in question). I have worked on awk and grep. sed awk grep whatever works is fine. – anotherperson1 Mar 14 '15 at 12:03
6

# is a special character. A special character is defined as a character that has a meaning beyond its literal meaning. # is used for commenting out in a Shell Script.

However there are exceptions, ie such as #! is referred to as a "sha-bang". A sha-bang is at the head of a script (the very first lines of code) tells your system that this file is a set of commands to be fed to the command interpreter indicated. Here are some:

 #!/bin/sh
 #!/bin/bash
 #!/usr/bin/perl
 #!/usr/bin/tcl
 #!/bin/sed -f
 #!/bin/awk -f

A command may not follow a comment on the same line. There is no method of terminating the comment, in order for "live code" to begin on the same line. Use a new line for the next command.

Here are some examples:

echo "The # here does not begin a comment."
echo 'The # here does not begin a comment.'
echo The \# here does not begin a comment.
echo The # here begins a comment.

echo ${PATH#*:}       # Parameter substitution, not a comment.
echo $(( 2#101011 ))  # Base conversion, not a comment.

You can alternatively google specific uses of # that you're unfamiliar with.

3

There's a lot of hash (#) uses in Kornshell beyond their use as a start of comments; $#, ${#}, ${var#pat}, ${var##pat}, ${#var}, ${#arr[*]}, ${#arr[@]}, base#number (i.e. literal numbers), and it can be part of patterns, e.g. [#], or literally in strings "...#...", or as part of the text data in "here-docs".

Newer versions of ksh will also support $( n <# ), ${var/#pat/str}, <<#, <# ((expr)), ># ((expr)), <#/pat, <##/pat, and # has a specific meaning in ksh's builtin printf format specifiers.

(I may still have missed one or two applications.)

References:

ksh93: http://www2.research.att.com/sw/download/man/man1/ksh.html

ksh88: http://www2.research.att.com/sw/download/man/man1/ksh88.html

Not the answer you're looking for? Browse other questions tagged or ask your own question.