Background: In Bash, $#
represents the number of arguments, and #
starts a comment.
Consider the following Bash script:
# This is a comment
while [[ $# -gt 0 ]]; do
echo $1
shift
done
Note that the syntax coloring by StackExchange here is incorrect! The #
in $#
is being treated as a comment marker.
Unfortunately, the listings
package gives me the same result:
\documentclass{article}
\usepackage{listings}
\lstset{language=Bash}
\pagenumbering{gobble}
\begin{document}
\begin{lstlisting}
# This is a comment
while [[ $# -gt 0 ]]; do
echo $1
shift
done
\end{lstlisting}
\end{document}
How can I fix this and get listings
to display the right output?
Best Answer
Whoever wrote the
listings
language forbash
probably didn't anticipate this case. Actually, thelistings
package is a far cry from a proper lexical analyser and doesn't offer a clean way of doing this kind of syntax highlighting. As far aslistings
'Bash
language is concerned, a#
character encountered in normal "processing" mode starts a comment, and that is it.An easy, if ugly, fix is to use the
literate
key to replace all instances of the$#
pattern by... well... itself... in order to prevent the#
character from starting a comment if preceded by$
:Unfortunately, this trick has side effects: if
columns=fullflexible
is used, space characters following$#
(if any) get gobbled, which is undesirable. One way to fix this is to use thekeepspaces
option, also.Update: Actually, the workaround for preventing spaces from getting gobbled that Manuel mentions in his answer is preferable to setting
keepspaces
; have a look.