Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i would be thankfull if you could help me how to figure out, how to determine if a variable's content starts with the hash sign:


myvar="#comment asfasfasdf"

if [ myvar = #* ] 

this does not work.



share|improve this question
I would consider the possibility that there may be spaces before the hash tag as well when testing for "comments", if that is indeed what you're doing. – rthomson Apr 14 '11 at 14:31
up vote 10 down vote accepted

One approach would be slicing off the first character of the variable's content, using "Substring Expansion":

if [[ ${x:0:1} == '#' ]]
    echo 'yep'
    echo 'nope'


From the Bash man page:

          Substring  Expansion.   Expands  to  up  to length characters of
          parameter starting at the character  specified  by  offset.   If
          length  is omitted, expands to the substring of parameter start-
          ing at the character specified by offset.  length and offset are
          arithmetic   expressions   (see  ARITHMETIC  EVALUATION  below).
          length must evaluate to a number greater than or equal to  zero.
          If  offset  evaluates  to  a number less than zero, the value is
          used as an offset from the end of the value  of  parameter.   If
          parameter  is  @,  the  result  is  length positional parameters
          beginning at offset.  If parameter is an array name indexed by @
          or  *,  the  result is the length members of the array beginning
          with ${parameter[offset]}.  A negative offset is taken  relative
          to  one  greater  than the maximum index of the specified array.
          Note that a negative offset must be separated from the colon  by
          at  least  one  space to avoid being confused with the :- expan-
          sion.  Substring indexing is zero-based  unless  the  positional
          parameters are used, in which case the indexing starts at 1.


Of course, your original approach would work just fine if you escaped the hash:

$ [[ '#snort' == \#* ]]; echo $?
share|improve this answer
thanks very much! was very helpful learning! – jan Mar 20 '11 at 0:41
To see this man page, I should type man what? – duleshi Mar 31 '14 at 4:17
It's the Bash man page, so "man bash" should do it. – Insyte Mar 31 '14 at 21:23

POSIX-compatible version:

[ "${var%${var#?}}"x = '#x' ] && echo yes


[ "${var#\#}"x != "${var}x" ] && echo yes


case "$var" in
    \#*) echo yes ;;
    *) echo no ;;
share|improve this answer

I know this may be heresy, but for this kind of things I'd rather use grep or egrep rather than doing it from within the shell. It's a little more costly (I guess) but for me this solution's readability offsets that. It's a matter of personal taste though, of course.


myvar="   #comment asfasfasdf"
if ! echo $myvar | egrep -q '^ *#'
  echo "not a comment"
  echo "commented out"

It works with or without leading spaces. If you'd like to account for leading tabs also, use egrep -q '^[ \t]*#' instead.

share|improve this answer
Thinking about it perhaps I just prefer egrep because I like regexes, but I consider grep/egrep to be useful in a wider arrange of situations anyway. – Eduardo Ivanec Apr 14 '11 at 14:46

Here is another way...

# assign to var the value of argument actual invocation
var=${1-"#default string"}

if [[ "$var" == "#"* ]]
  echo "$line starts with a #"

Just copy paste the content to a file, grant execution permissions, and watch how it just works ;).

Hope it helps!


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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