# Emulating a shell environment

I'm trying to define a new environment, which emulates a shell. The way I would like to use it:

\begin{shell}
\item{echo foo}{foo}
\item{echo bar}{bar}
\end{shell}


Which should produce:

This is my shell prompt
1> echo foo
foo
2> echo bar
bar


What I have so far:

\newcommand{shellprompt}{This is my shell prompt}
\newenvironment{shell}{
\shellprompt
\begin{enumerate}[\hspace{15px} 1\textgreater]
\let\olditem\item
\renewcommand\item{\olditem}
}{
\end{enumerate}
}


How do I redefine the \item command so it accepts a second parameter and prints it on a second line?

Bonus: How do I wrap everything (prompt + items) in a verbatim environment?

-
Why \item? You can define your own custom macro with any number of arguments, instead of redefining a macro already used by latex for other purposes. –  JLDiaz Jul 23 '12 at 12:14

This is a possibility:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb}
\DefineVerbatimEnvironment{shell}{Verbatim}{commandchars=\%\{\},formatcom=\setcounter{prompt}{0}}
\newcounter{prompt}
\newcommand{\prompt}{\stepcounter{prompt}\theprompt>}

\begin{document}
\begin{shell}
%prompt echo foo
foo
%prompt echo bar
bar
\end{shell}
\end{document}


In the shell command you'll need to escape %, { and } with a %, so, to print

% { }


you have to input

%% %{ %}


If you want to add the same first line to all environments,

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb}
\DefineVerbatimEnvironment{shell}{Verbatim}{commandchars=\%\{\},formatcom=\setcounter{prompt}{0}\start}
\newcounter{prompt}
\newcommand{\prompt}{\stepcounter{prompt}\theprompt>}
\newcommand{\start}{\noindent This is my shell prompt\par}

\begin{document}
\begin{shell}
%prompt echo foo{}
foo
%prompt echo bar
bar
\end{shell}
\end{document}


Just redefine \start to suit.

-
Right. But what about the prompt used as "header" (the "This is my shell prompt" line shown only once for each shell environment)? –  Roberto Aloi Jul 23 '12 at 13:20
Awesome. I've also realized that the fancyvrb package supports labels, which for me represents a valid alternative for the "header". Thanks so much. –  Roberto Aloi Jul 23 '12 at 13:59
The start command trick doesn't work well when a frame is used in the environment. –  Roberto Aloi Jul 24 '12 at 10:08
@RobertoAloi No, it doesn't. :( –  egreg Jul 24 '12 at 10:23