文件

2.4 KiB

psh - A minimal bash-like shell in pure C

A lightweight POSIX shell implementation featuring:

  • Command execution via fork/exec (Windows: CreateProcess)
  • Built-in commands: cd, pwd, echo, exit, help, env
  • Input/output redirection (<, >, >>)
  • Piping (|)
  • Background execution (&)
  • Command history with up/down arrows
  • Tab completion
  • && and || operators
  • Basic environment variable support
  • Tilde expansion (~)

Building

make

Requires:

  • GCC or compatible C compiler
  • GNU Readline library (for line editing)

Running

./psh

Features

Built-ins

  • cd [dir] - Change directory
  • pwd - Print working directory
  • echo [args] - Print text (supports $VAR expansion)
  • exit - Exit shell
  • help - Show help
  • env [var=val] - Show or set environment variables
  • history - Show command history

Redirection

ls > output.txt      # Redirect output
cat < input.txt      # Redirect input
echo hi >> log.txt   # Append output

Piping

ps aux | grep python | wc -l

Background

sleep 100 &

Logical Operators

cmd1 && cmd2    # Execute cmd2 only if cmd1 succeeds
cmd1 || cmd2    # Execute cmd2 only if cmd1 fails

Tab Completion

Press Tab to complete commands:

  • Built-in commands (cd, pwd, echo, etc.)
  • Executable files in PATH and current directory
  • Environment variables

Command History

  • ↑/↓ arrows: Navigate history
  • Tab: Try completion
  • Backspace: Delete character

Example Session

user@localhost:F:\Paze SH> echo Hello World
Hello World

user@localhost:F:\Paze SH> ls && echo "List completed"
psh.c  README.md  Makefile
List completed

user@localhost:F:\Paze SH> false || echo "Fallback executed"
Fallback executed

user@localhost:F:\Paze SH> help
psh - minimal bash-like shell (Windows)

Built-in commands:
  cd [dir]     Change directory
  pwd          Print working directory
  echo [args]  Print arguments
  exit         Exit shell
  env [var=val] Show/set environment
  help         Show this help

Features:
  cmd1 | cmd2       Pipe output
  cmd > file        Redirect output
  cmd >> file       Append output
  cmd < file        Redirect input
  cmd &             Run in background
  cmd1 && cmd2      Execute cmd2 if cmd1 succeeds
  cmd1 || cmd2      Execute cmd2 if cmd1 fails
  history           View command history
  Tab               Try command completion

License

MIT