A cross-platform C# library for executing bash commands with strongly-typed results. Works on Windows (via WSL), Linux, and macOS.
$ dotnet add package BashSharpA cross-platform C# library for executing bash commands with strongly-typed results. Works on Windows (via WSL), Linux, and macOS.
Install via NuGet:
dotnet add package BashSharp
// Simple execution - returns success/failure
bool success = await BashCommandService.ExecuteCommand("echo 'Hello World'");
// Get exit code
int exitCode = await BashCommandService.ExecuteCommandWithCode("ls -la");
// Get strongly-typed results
public class LsResult : ICommandResult
{
public List<string> Files { get; private set; } = new();
public int ExitCode { get; private set; }
public void SetExitCode(int exitCode) => ExitCode = exitCode;
public void ParseResult(string output)
{
Files = output.Split('\n')
.Where(line => !string.IsNullOrWhiteSpace(line))
.ToList();
}
public void ParseError(string error) { }
}
var result = await BashCommandService.ExecuteCommandWithResults<LsResult>("ls");
foreach (var file in result.Files)
{
Console.WriteLine(file);
}
// With timeout
await BashCommandService.ExecuteCommand("long-running-command", timeoutMs: 5000);
// With cancellation
using var cts = new CancellationTokenSource();
var task = BashCommandService.ExecuteCommand("long-running-command", cancellationToken: cts.Token);
// Cancel after 1 second
await Task.Delay(1000);
cts.Cancel();The library throws exceptions when commands fail or return non-zero exit codes. Use try-catch blocks to handle errors:
try
{
await BashCommandService.ExecuteCommand("invalid-command");
}
catch (Exception ex)
{
Console.WriteLine($"Command failed: {ex.Message}");
}wsl --install -d Debianwsl --set-default-version 2Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)This project is licensed under the terms of the LICENSE file included in the repository.