Table of Contents
Built-in Commands
exit [code]
Exit the shell with optional exit code.
exit # Exit with code 0
exit 1 # Exit with code 1
cd [dir]
Change directory. Defaults to home directory if no argument provided.
cd # Change to home directory
cd /path/to/dir # Change to specific directory
cd .. # Change to parent directory
pwd
Print the current working directory.
pwd
echo [args...]
Print arguments to standard output.
echo "Hello World"
echo $MY_VAR
echo "Multiple" "arguments"
env
Show all environment variables.
env
export VAR=value
Set an environment variable that will be available to child processes.
export MY_VAR="Hello"
export PATH="$PATH:/new/path"
local VAR=value
Set a local variable that exists only within the current shell session (not exported to child processes).
local TEMP_VAR="temporary value"
local COUNT=42
local RESULT="$(date +%Y%m%d)"
unset VAR
Unset (remove) an environment variable.
unset MY_VAR
alias name=command
Create a command alias.
alias ll="ls -la"
alias grep="grep --color=auto"
unalias name
Remove an alias.
unalias ll
history
Show command history.
history
which cmd
Show the path to a command.
which ls
which python
help
Show help information.
help
functions
List all defined functions.
functions
type name
Show the definition of a function or command type.
type my_function
type ls
Cross-Platform File Operations
Gosh supports both Unix-style and Windows-style commands for file operations, making it easy to work across different platforms.
ls
/ dir
- List Directory Contents
List files and directories. Both Unix (ls
) and Windows (dir
) style commands are supported.
# Unix style
ls
ls -l # Long format
ls -la # Long format with hidden files
ls -lh # Long format with human-readable sizes
# Windows style
dir
dir -l # Long format
dir -la # Long format with hidden files
dir -lh # Long format with human-readable sizes
cat
/ type
- Display File Contents
Display the contents of files. Both Unix (cat
) and Windows (type
) style commands work.
# Unix style
cat file.txt
cat file1.txt file2.txt # Multiple files
# Windows style
type file.txt
type file1.txt file2.txt # Multiple files
cp
/ copy
- Copy Files
Copy files and directories. Supports both Unix and Windows syntax.
# Unix style
cp source.txt dest.txt
cp -r source_dir dest_dir # Recursive copy
# Windows style
copy source.txt dest.txt
copy -r source_dir dest_dir
mv
/ move
- Move/Rename Files
Move or rename files and directories.
# Unix style
mv oldname.txt newname.txt
mv file.txt /new/location/
# Windows style
move oldname.txt newname.txt
move file.txt /new/location/
rm
/ del
- Remove Files
Delete files and directories.
# Unix style
rm file.txt
rm -r directory/ # Recursive removal
rm -rf directory/ # Force recursive removal
# Windows style
del file.txt
del -r directory/ # Recursive removal
del -rf directory/ # Force recursive removal
mkdir
/ md
- Create Directories
Create new directories.
# Unix style
mkdir new_directory
mkdir -p path/to/nested/dir # Create parent directories
# Windows style
md new_directory
md -p path/to/nested/dir # Create parent directories
rmdir
/ rd
- Remove Directories
Remove empty directories.
# Unix style
rmdir empty_directory
# Windows style
rd empty_directory
clear
/ cls
- Clear Screen
Clear the terminal screen.
# Unix style
clear
# Windows style
cls
Command Features
Command Chaining
Execute multiple commands sequentially with ;
:
cmd1; cmd2; cmd3
ls; pwd; echo "Done"
Unix Pipes
Connect commands with |
to chain operations:
cmd1 | cmd2
ls | grep ".go" | wc -l
Input/Output Redirection
cmd < input.txt
- Redirect input from filecmd > output.txt
- Redirect output to file (overwrite)cmd >> output.txt
- Redirect output to file (append)
# Input redirection
cat < input.txt
sort < unsorted.txt
# Output redirection
echo "Hello" > output.txt
ls -la > file_list.txt
# Append to file
echo "World" >> output.txt
date >> log.txt
Background Execution
Run commands in background with &
:
sleep 10 &
long_running_process &
Quote Handling
Support for single and double quotes:
echo "Hello World"
echo 'Single quotes'
echo "Variable: $HOME"
Comment Support
Lines starting with #
are treated as comments in scripts:
# This is a comment
echo "This is not a comment"
Variable Expansion
Expand environment variables with $VAR
and ${VAR}
syntax:
echo $HOME
echo ${HOME}/Documents
echo "Path: $PATH"
Command Substitution
Execute commands and use their output with $(command)
and `command`
:
echo "Current directory: $(pwd)"
echo "Today is $(date +%Y-%m-%d)"
echo "Files: `ls | wc -l`"
Tilde Expansion
Expand ~
to home directory and user paths with cross-platform path separator support:
# Unix-style paths (work everywhere)
ls ~/Documents
cp file.txt ~/backup/
cd ~user/projects
# Windows-style paths (work on Windows)
ls ~\Documents
cp file.txt ~\backup\
cd ~user\projects
Glob Patterns
Use wildcards for filename matching:
# Match all .go files
ls *.go
# Match single character
rm temp_file_?.txt
# Character classes
echo [Hh]ello*
ls [abc]*
Functions
Define and use custom functions with positional parameters:
# Define a function
greet() {
echo "Hello, $1!"
echo "You passed $# arguments"
}
# Call the function
greet "World"
greet "Alice" "Bob" "Charlie"
# List functions
functions
# Show function definition
type greet
Examples
Cross-Platform File Operations
# List files (both styles work)
ls -la
dir -l
# Display file contents
cat README.md
type README.md
# Copy files
cp source.txt backup.txt
copy source.txt backup.txt
# Create and navigate directories
mkdir project
cd project
ls
Basic Operations
# Navigate and list files
cd /usr/local
ls -la
pwd
# Work with files
echo "Hello" > greeting.txt
cat greeting.txt
cp greeting.txt backup.txt
Pipes and Redirection
# Count Go files
ls | grep ".go" | wc -l
# Find and sort
find . -name "*.txt" | sort > text_files.txt
# Process logs
cat access.log | grep "ERROR" | tail -10
Environment Variables
# Set and use variables
export PROJECT_DIR="/Users/dev/projects"
cd $PROJECT_DIR
echo "Working in: $PROJECT_DIR"
# Modify PATH
export PATH="$PATH:$HOME/bin"
which my_script
Command Substitution
# Use command output
echo "Backup created: backup_$(date +%Y%m%d).tar"
mkdir "project_$(whoami)"
echo "Found $(ls *.go | wc -l) Go files"
Functions
# Define utility functions
backup_file() {
cp "$1" "$1.backup.$(date +%Y%m%d)"
echo "Backed up $1"
}
log_message() {
echo "[$(date '+%H:%M:%S')] $@"
}
# Use functions
backup_file "important.txt"
log_message "Process started"
log_message "Processing" "multiple" "arguments"
Complex Workflows
# Backup and compress
tar -czf "backup_$(date +%Y%m%d).tar.gz" ~/Documents
# Search and replace
grep -r "old_function" . | cut -d: -f1 | sort | uniq
# System monitoring
ps aux | grep gosh | awk '{print $2, $11}' | head -5