/Shell.NET

Utilize Bash shell directly in .NET Core

Primary LanguageC#MIT LicenseMIT

Shell.NET NuGet Build Status

Now Windows compatible using the Linux Subsystem

Installation

Use the .NET CLI to get this library! Prereleases need an explicit version.

dotnet add package Shell.NET

Or add the following to your .csproj:

<ItemGroup>
  <PackageReference Include="Shell.NET" Version="0.2.2" />
</ItemGroup>

Example

var bash = new Bash();
bash.Grep("export", "~/.bashrc", redirect: false);

// Commands return a BashResult that stores output information:
if (bash.Rm("~/Desktop/bashrc-backup").ExitCode == 0)
    Console.WriteLine("Success!");

// With redirect (default in most commands), access the command's output from BashResult.Output:
var bashrc = bash.Cat("~/.bashrc").Output;

// Without redirect, the command's output gets printed to the terminal:
bash.Cat("~/.bashrc", redirect: false);

// BashResult.Lines splits BashResult.Output by new-lines and stores the result as an array:
foreach (var line in bash.Ls("-lhaF").Lines)
    Console.WriteLine(line);

// Run custom commands using Bash.Command():
var libs = bash.Command("ldd /usr/bin/dotnet").Lines;
Console.WriteLine($"OS: {bash.Command("uname -s").Output}");

Details

Bash commands return an instance of BashResult that stores redirected output information in BashResult.Output, BashResult.ErrorMsg, BashResult.ExitCode, and BashResult.Lines. By default, all commands (except for Bash.Echo()) will redirect their output information. If a command is run with redirect: false, all properties in BashResult except for BashResult.ExitCode will be null.