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:
#!bin/sh
myvar="#comment asfasfasdf"
if [ myvar = #* ]
this does not work.
Thanks!
Jans
|
|
One approach would be slicing off the first character of the variable's content, using "Substring Expansion":
From the Bash man page:
EDIT: Of course, your original approach would work just fine if you escaped the hash:
|
||||
|
|
|
POSIX-compatible version:
or:
or:
|
||||
|
|
|
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. So:
It works with or without leading spaces. If you'd like to account for leading tabs also, use egrep -q '^[ \t]*#' instead. |
||||
|