You can use numberblanklines=false
; a little example:
\documentclass{article}
\usepackage{listings}
\lstset{numbers=left,numberblanklines=false}
\begin{document}
\begin{lstlisting}
First line.
Second line.
\end{lstlisting}
\end{document}
The above approach will not number blank lines, but will take them into account for increasing the counter controlling the line numbering; you can define a command to decrease the counter manually; something like this:
\documentclass{article}
\usepackage{listings}
\lstset{numbers=left,numberblanklines=false,escapeinside=||}
\newcommand*\DNumber{\addtocounter{lstnumber}{-1}}
\begin{document}
\begin{lstlisting}
First line.|\DNumber|
Second line.|\DNumber|
Third line.
\end{lstlisting}
\end{document}
Marco Daniel suggested the following automated way of preventing blank lines from increasing the counter, by defining a new key countblanklines
and using the macro \lst@AddToHook{<hook>}
:
\documentclass{article}
\usepackage{listings}
\lstset{numbers=left,numberblanklines=false}
\makeatletter
\lst@Key{countblanklines}{true}[t]%
{\lstKV@SetIf{#1}\lst@ifcountblanklines}
\lst@AddToHook{OnEmptyLine}{%
\lst@ifnumberblanklines\else%
\lst@ifcountblanklines\else%
\advance\c@lstnumber-\@ne\relax%
\fi%
\fi}
\makeatother
\begin{document}
\begin{lstlisting}
First line.
Second line.
Third.
\end{lstlisting}
\begin{lstlisting}[countblanklines=false]
First line.
Second line.
Third.
\end{lstlisting}
\end{document}
A slightly different approach from Heiko's fine answer:
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstnewenvironment{bash}[1][]
{\lstset{language=C}\lstset{%
numbers=left,numberstyle=\normalsize,stepnumber=1,numbersep=5pt,
framesep=5pt,
basicstyle=\normalsize\ttfamily,
showstringspaces=false,
keywordstyle=\itshape\color{blue},
stringstyle=\color{maroon},
commentstyle=\color{black},
xleftmargin=5pt,
xrightmargin=5pt,
aboveskip=\bigskipamount,
belowskip=\bigskipamount,
backgroundcolor=\color{gray!20}, #1
}}
{}
%
%%% Always I forget this so I created some aliases
\def\ContinueLineNumber{\lstset{firstnumber=last}}
\def\StartLineAt#1{\lstset{firstnumber=#1}}
\let\numberLineAt\StartLineAt
\begin{document}
\begin{bash}[name=Test]
echo xyz
\end{bash}
\StartLineAt{30}
\begin{bash}[name=Test]
echo xyz
\end{bash}
\ContinueLineNumber
\begin{bash}[name=Test]
echo xyz
\end{bash}
\end{document}
The code offers two macros that I personally find convenient
\ContinueLineNumber
and
\StartLineAt
If you wouldn't like all your code to have consecutive numbering you might find it useful.
Best Answer
It isn't such simple as it sounds. You have to hook every line. Here a small solution which needs more testing:
Next time please provide an example. I took this one from you previous question.