With thanks to Kit Baum my package inshell has been updated on the SSC. Though this package was originally released in December 2021, I wanted to wait until I made some improvements before I announced it here on this forum.
This package is a wrapper of shell that will capture the standard output, standard error, and native shell return codes of commands sent to the operating system's shell through Stata. It works on macOS, Linux, and Microsoft Windows, although it has not been thoroughly tested on the latter.
It is an improvement over other shell wrappers in several ways. For instance, ashell, with all due respect to its author Nikos Askitas and which seems to be the predominant package of its kind, does not properly handle the commands it sends to the shell. It has been in use since 2007 and was last updated in 2009 and I am surprised that no one has noticed this limitation. Because shell code (with the exception of Windows Command shell) uses the same dollar sign alphanumeric notation for its variables that Stata uses for its own global macros, commands sent via a shell wrapper must prevent macro substitution, which inshell does at every level with prodigious use of the macval(lclname) expansion operator.
For an example of this, try (on macOS and Linux systems):
inshell will also capture and display the standard error and the shell's native return code (not to be confused with c(rc) or _rc, Stata's internal return code) if the commands send to it produce an error. It displays these error details using a simple "theme" like so:
The last command and its resulting error in the above screenshot is more than just a silly demonstration; it not only shows that inshell can handle Unicode (UTF-8) strings (with proper display column sizing) but that it can even correctly parse expressions that Stata would otherwise interpret as its own local macros. In testing this package I somewhat randomly discovered that the cal command-line utility outputs its error with this unusual quoting that mirrors Stata's local macro structure, and though this specific case is unlikely to be relevant to any of inshell's users, my discovery of it led to a desire to make inshell that much more robust.
Users of shell in all its forms are encouraged to read the help file, as it is a good primer on the advanced usage of the shell through Stata.
The package also includes (of all things) a wrapper of cd. I added this subprogram primarily to allow users of inshell to cd to directories stored as shell variables. As shell cannot cd to any directories and cd will not reference variables within the shell's environment, this allows the user to use inshell as a more complete solution in their code.
It has also been tested with Microsoft PowerShell and the help file contains information regarding its use:
Because the output of shell commands can often contain ANSI escape sequences, which are meant to be interpreted as display directives and can garble the results that are captured and printed, it also contains a subprogram that will remove these if it detects them. This is important because these control sequences are often invisible and can cause headaches when passed to other commands. For example, if the above PowerShell command is sent through shell, the results might look something like this:
and if the user enters such a command using the GUI version of Stata and selects the text in the Results window they may witness the text visibly shift. As stated above these aberrations can become more than just visual when they are captured into macros and subsequently parsed.
Hopefully some of you will find this program useful. Please don't hesitate to contact me here on this forum or via email with comments or suggestions.
Best,
Matthew Hall
This package is a wrapper of shell that will capture the standard output, standard error, and native shell return codes of commands sent to the operating system's shell through Stata. It works on macOS, Linux, and Microsoft Windows, although it has not been thoroughly tested on the latter.
It is an improvement over other shell wrappers in several ways. For instance, ashell, with all due respect to its author Nikos Askitas and which seems to be the predominant package of its kind, does not properly handle the commands it sends to the shell. It has been in use since 2007 and was last updated in 2009 and I am surprised that no one has noticed this limitation. Because shell code (with the exception of Windows Command shell) uses the same dollar sign alphanumeric notation for its variables that Stata uses for its own global macros, commands sent via a shell wrapper must prevent macro substitution, which inshell does at every level with prodigious use of the macval(lclname) expansion operator.
For an example of this, try (on macOS and Linux systems):
inshell will also capture and display the standard error and the shell's native return code (not to be confused with c(rc) or _rc, Stata's internal return code) if the commands send to it produce an error. It displays these error details using a simple "theme" like so:
The last command and its resulting error in the above screenshot is more than just a silly demonstration; it not only shows that inshell can handle Unicode (UTF-8) strings (with proper display column sizing) but that it can even correctly parse expressions that Stata would otherwise interpret as its own local macros. In testing this package I somewhat randomly discovered that the cal command-line utility outputs its error with this unusual quoting that mirrors Stata's local macro structure, and though this specific case is unlikely to be relevant to any of inshell's users, my discovery of it led to a desire to make inshell that much more robust.
Users of shell in all its forms are encouraged to read the help file, as it is a good primer on the advanced usage of the shell through Stata.
The package also includes (of all things) a wrapper of cd. I added this subprogram primarily to allow users of inshell to cd to directories stored as shell variables. As shell cannot cd to any directories and cd will not reference variables within the shell's environment, this allows the user to use inshell as a more complete solution in their code.
It has also been tested with Microsoft PowerShell and the help file contains information regarding its use:
Because the output of shell commands can often contain ANSI escape sequences, which are meant to be interpreted as display directives and can garble the results that are captured and printed, it also contains a subprogram that will remove these if it detects them. This is important because these control sequences are often invisible and can cause headaches when passed to other commands. For example, if the above PowerShell command is sent through shell, the results might look something like this:
and if the user enters such a command using the GUI version of Stata and selects the text in the Results window they may witness the text visibly shift. As stated above these aberrations can become more than just visual when they are captured into macros and subsequently parsed.
Hopefully some of you will find this program useful. Please don't hesitate to contact me here on this forum or via email with comments or suggestions.
Best,
Matthew Hall

Comment