# Why does the simplest \newenvironment not work as expected? [duplicate]

\documentclass{article}
\usepackage{listings}

\newenvironment{hello}{\begin{lstlisting}[language=sh]}{\end{lstlisting}}

\begin{document}

\begin{hello}
ls
\end{hello}

\end{document}


The code cannot be compiled by xelatex, the compiler is hung.

What's the root cause?

listings is essentially a verbatim environment that turns off normal latex processing, in particular \end{hello} does not end the environment.

The package has facilities for defining custom environments, see section 4.16 of the listings manual.

\documentclass{article}
\usepackage{listings}

\lstnewenvironment{hello}{\lstset{language=sh}}{}

\begin{document}

\begin{hello}
ls
\end{hello}

\end{document}