nsh is a small Unix-like shell written in C. It supports external command execution plus a set of built-ins for job control, history, conditionals, and variables.
Requirements:
- CMake 3.11+
- A C compiler with C11 support
From the repository root:
cmake -B ./build
cmake --build ./build --target nsh -- -j 4
./build/nshThe shell prompt is printed as:
<user>@<host>:<cwd>
$
If the current working directory is inside your home directory, the home prefix is displayed as ~.
- Tokens are space-separated.
- Multiple spaces are accepted.
- Text in double quotes is treated as a single argument.
#starts a comment (ignored from that point to end of line).~expands to your home directory when used as a path token prefix.
Any executable in PATH can be run:
ls -la
pwd
cat file.txt>redirect stdout (truncate/create)>>redirect stdout (append/create)<redirect stdin
Examples:
echo hello > out.txt
echo world >> out.txt
wc -l < out.txtUse | to connect stdout of one command to stdin of the next.
cat file.txt | grep error | wc -l;run commands sequentially&&run right side only if left side succeeds (exit status 0)||run right side only if left side fails (non-zero exit status)
Examples:
mkdir testdir ; cd testdir
grep needle file.txt && echo found
grep needle file.txt || echo not-foundAppend & to run a command line in background:
sleep 30 &Built-ins:
jobslist active background jobsfg [n]wait for a background job in foreground- without argument: last background job
- with argument: job index from
jobs
Examples:
jobs
fg
fg 1History is persisted at:
~/.nsh_history
historyprints recent commands (up to 10 retained)again <n>re-executes history entry numbern
Examples:
history
again 3Note: if a line starts with a leading space, it is not added to history.
if is a built-in with this structure:
if <condition>
then
<commands>
else
<commands>
end
thenblock is optional.elseblock is optional.- Nested
ifis supported.
Single-line example:
if grep -q main main.c then echo yes else echo no endYou can also use helper built-ins:
truereturns success (0)falsereturns failure (1)
Example:
if true then echo ok else echo bad endVariables are managed with built-ins:
setshow all variablesset <key> <value...>set/update variableget <key>print variable valueunset <key>remove variable
Examples:
set name nsh
get name
unset name
setCommand substitution in set:
set files `ls`
get filesThe output of the command inside backticks is stored as the variable value.
Commands executed directly by the shell process:
cd <dir>change current directoryexitleave the shellfg [n]set [key value...]unset <key>
Built-ins available in command execution/pipelines:
jobshistorytruefalseif ... then ... [else ...] endhelp [topic]get <key>
Inside nsh, run:
helpor:
help <topic>Available topics include:
basicmulti-pipebackgroundspaceshistoryctrl+cchainifmulti-ifhelpvariables
Ctrl+C(SIGINT) is handled by the shell.- Foreground command handling attempts graceful interruption and escalates when needed.
- Background jobs are managed separately.