/bin/sh starts with check in script

Jan Henrik Sylvester me at janh.de
Thu Feb 11 14:29:28 UTC 2016

On 02/10/2016 13:58, Sergei G wrote:
> I came up with this solution to check if variable $line starts with a
> hash.  Basically I am checking if line is a comment in the configuration
> file.
> #!/bin/sh
> if expr "${line}" : '#.*' > /dev/null; then
>   echo Ignoring comment line
> fi
> I had to redirect to /dev/null, because expr prints a number to STDOUT. 
> Is there a better way to do this kind of string matching check in
> /bin/sh (not bash)?

[ "${line#\#}" != "$line" ] && echo comment

See the Parameter Expansion section of sh(1).

Jan Henrik

More information about the freebsd-questions mailing list