Skip to content

krikera/python-shell-exp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Shell Emulator

This project is a lightweight command-line shell emulator written in Python. It implements basic command execution, tab completion, redirection features, command history, and pipeline functionality similar to those found in traditional Unix shells.

Features

  • Tab Completion:
    Automatically completes both built-in commands and external executables found in your system's PATH.

  • Command History:

    • Tracks commands entered during the current and previous sessions
    • Navigate through history using up/down arrow keys
    • View command history with the history command
    • Limit history display with history N (shows last N commands)
    • History persists between shell sessions in ~/.python_shell_history
    • Supports redirection of history output to files
  • Built-in Commands:
    Supports a handful of common built-ins including:

    • type: Identifies whether a command is a shell built-in or an external executable.
    • cd: Changes the current working directory.
    • exit: Exits the shell.
    • pwd: Prints the current working directory.
    • echo: Displays text on the terminal.
    • export: Sets or displays environment variables.
    • history: Displays command history.
    • help: Displays information about built-in commands.
  • Command Redirection:
    Basic support for redirecting input, output, and errors:

    • > or 1>: Redirect standard output to a file (write mode).
    • >> or 1>>: Redirect standard output to a file (append mode).
    • 2>: Redirect standard error to a file (write mode).
    • 2>>: Redirect standard error to a file (append mode).
    • <: Redirect standard input from a file.
  • Pipes:
    Connect multiple commands together with the pipe operator (|):

    • Redirect the output of one command as input to another
    • Chain multiple commands together in a pipeline
    • Combine pipes with redirections for complex command sequences
    • Both built-in and external commands support piping
  • Environment Variables:
    Full support for environment variable management:

    • Set variables using export VAR=value.
    • Use variables in commands with $VAR or ${VAR} syntax.
    • Access special variables like $$ (process ID) and $? (exit code).
    • Variables persist throughout the shell session.
    • View all variables using export without argument.
  • Tilde Expansion:
    Support for using ~ as a shortcut:

    • Standalone ~ expands to the user's home directory
    • Paths starting with ~/ expand to paths relative to home
    • ~username/ expands to another user's home directory
    • Works in all contexts where paths are expected
  • Help System:
    Documentation for built-in commands:

    • help lists all available built-in commands
    • help command provides detailed help on a specific command
    • Includes syntax, description, and usage information
    • Supports redirection and can be used in pipes
  • External Command Execution:
    Executes external commands by searching for them in your system's PATH with proper error handling for cases like command not found or permission issues.

Requirements

  • Python 3.x
  • Standard Python libraries:
    • sys
    • os
    • shutil
    • subprocess
    • shlex
    • readline
    • re
    • atexit

Usage

  1. Run the Shell:
    Execute the script from the terminal:

    python main.py
  2. Environment Variable Examples:

    • Set a variable:

      export NAME=World
    • Use variables in commands:

      echo "Hello $NAME"
    • Check exit codes:

      ls /nonexistent
      echo $?
  3. Command History Examples:

    • View all command history:

      history
    • View last 5 commands:

      history 5
    • Save history to a file:

      history > my_history.txt
    • Navigate history: Press up/down arrow keys to cycle through previous commands

  4. Pipe Examples:

    • Basic pipe to filter output:

      ls | grep .py
    • Chain multiple commands:

      ls -la | grep "^d" | sort
    • Combine pipes with redirections:

      ls | grep .txt > text_files.txt
    • Use built-in commands in pipes:

      echo "Hello World" | grep "Hello"
    • Count items with pipes:

      ls | wc -l
  5. Tilde Expansion Examples:

    • Navigate to home directory:

      cd ~
    • List files in home subdirectory:

      ls ~/Documents
    • Create file in home directory:

      echo "test" > ~/testfile.txt
  6. Help Examples:

    • View available commands:

      help
    • Get help on specific command:

      help cd
      help export
    • Save command help to file:

      help echo > echo_help.txt
    • Search through command help:

      help | grep directory

About

A lightweight command-line shell emulator written in Python.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages