# NuttShell (NSH) Last Updated: September 1, 2011

# Table of Contents

| 1.0 Overview
| 1.1 Console/NSH Front End
| 1.2 Command Overview
| 1.3 Conditional Command Execution
| 1.4 Built-In Variables
| 1.5 Current Working Directory
| 1.6 Environment Variables
| 1.7 NSH Start-Up Script
| 2.0 Commands.
| 2.1 Evaluate Expression (test)
| 2.2 Concatenate Files (cat)
| 2.3 Change Current Working Directory (cd)
| 2.4 Copy Files (cp)
| 2.5 Show or set the date and time (date)
| 2.6 Copy and Convert Files (dd)
| 2.7 Echo Strings and Variables (echo)
| 2.8 Execute User Code (exec)
| 2.9 Exit NSH (exit)
| 2.10 Show Memory Manager Status (free)
| 2.11 Get File Via TFTP (get)
| 2.12 Show Usage Command Usage (help)
| 2.13 Show Network Configuration (ifconfig)
| 2.14 Send a signal to a task (kill)
| 2.15 Setup/teardown the Loop Device (losetup)
| 2.16 List Directory Contents (ls)
| 2.17 Access Memory (mb, mh, and mw)
| 2.18 Show Current Tasks and Threads (ps)
| 2.19 Create a Directory (mkdir)
| 2.20 Create a FAT Filesystem (mkfatfs)
| 2.21 Create a FIFO (mkfifo)
| 2.22 Create a RAMDISK (mkrd)
| 2.23 Mount a File System (mount)
| 2.24 Check Network Peer (ping)
| 2.25 Send File Via TFTP (put)
| 2.26 Show Current Working Directory (pwd)
| 2.27 Remove a File (rm)
| 2.28 Remove a Directory (rmdir)
| 2.29 Set an Environment Variable (set)
| 2.30 Execute an NSH Script (sh)
| 2.31 Wait for Seconds (sleep)
| 2.32 Unmount a File System (umount)
| 2.33 Unset an Environment Variable (unset)
| 2.34 Wait for Microseconds (usleep)
| 2.35 Get File Via HTTP (wget)
| 2.36 Hexadecimal Dump (xd)
| 3.0 Configuration Settings
| 3.1 Command Dependencies on Configuration Settings
| 3.2 NSH-Specific Configuration Settings
| Index

# 1.0 Overview

The `apps/nshlib` sub-directory contains the NuttShell (NSH) library. This library can easily to linked to produce a NSH application (See as an example `apps/nshlib`). NSH is a simple shell application for NuttX.

## 1.1 Console/NSH Front End

Using settings in the configuration file, NSH may be configured to use either the serial stdin/out or a telnet connection as the console or BOTH. When NSH is started, you will see the following welcome on either console:

    NuttShell (NSH)
    nsh>

`nsh>` is the NSH prompt and indicates that you may enter a command from the console.

## 1.2 Command Overview

Simple, Re-directed, and Background Commands. The NuttShell (NSH) is a simple shell application. NSH supports the following commands forms:

Simple command: | `<cmd>`
Command with re-directed output: | `<cmd> > <file> <cmd> >> <file>`
Background command: | `<cmd> &`
Re-directed background command: | `<cmd> > <file> & <cmd> >> <file> &`

Where:

`<cmd>` | is any one of the simple commands listed later.
`<file>` | is the full or relative path to any writable object in the filesystem name space (file or character driver). Such objects will be referred to simply as files throughout this document.

`nice`'d Background Commands NSH executes at the mid-priority (128). Backgrounded commands can be made to execute at higher or lower priorities using `nice`:

`[nice [-d <niceness>>]] <cmd> [> <file>|>> <file>] [&]`

Where `<niceness>` is any value between -20 and 19 where lower (more negative values) correspond to higher priorities. The default niceness is 10.

## 1.3 Conditional Command Execution

An `if-then[-else]-fi` construct is also supported in order to support conditional execution of commands. This works from the command line but is primarily intended for use within NSH scripts (see the `sh` commnd). The syntax is as follows:

    if <cmd>
    then
      [sequence of <cmd>]
    else
      [sequence of <cmd>]
    fi

## 1.4 Built-In Variables

`$?` | The result of the last simple command execution. On backgrounded commands, this variable holds only the result of spawning the background command.

## 1.5 Current Working Directory

`cd` and `pwd`. All path arguments to commands may be either an absolute path or a path relative to the current working directory. The current working directory is set using the `cd` command and can be queried either by using the `pwd` command or by using the `echo` `$PWD` command.

## 1.6 Environment Variables

Environment Variables:

`PWD` | The current working directory
`OLDPWD` | The previous working directory

## 1.7 NSH Start-Up Scrip

NSH Start-Up Script. NSH supports options to provide a start up script for NSH. In general this capability is enabled with `CONFIG_NSH_ROMFSETC`, but has several other related configuration options as described with the NSH-specific configuration settings. This capability also depends on:

- `CONFIG_DISABLE_MOUNTPOINT` not set
- `CONFIG_NFILE_DESCRIPTORS` < 4
- `CONFIG_FS_ROMFS` enabled

Default Start-Up Behavior. The implementation that is provided is intended to provide great flexibility for the use of Start-Up files. This paragraph will discuss the general behavior when all of the configuration options are set to the default values.

In this default case, enabling `CONFIG_NSH_ROMFSETC` will cause NSH to behave as follows at NSH startup time:

- NSH will create a read-only RAM disk (a ROM disk), containing a tiny ROMFS filesystem containing the following:

      `--init.d/
           `-- rcS

  Where rcS is the NSH start-up script.
- NSH will then mount the ROMFS filesystem at `/etc`, resulting in:

      |--dev/
      |   `-- ram0
      `--etc/
          `--init.d/
              `-- rcS

- By default, the contents of rcS script are:

      # Create a RAMDISK and mount it at XXXRDMOUNTPOUNTXXX

      mkrd -m 1 -s 512 1024
      mkfatfs /dev/ram1
      mount -t vfat /dev/ram1 /tmp

- NSH will execute the script at `/etc/init.d/rcS` at start-up (before the first NSH prompt. After execution of the script, the root FS will look like:

      |--dev/
      |   |-- ram0
      |   `-- ram1
      |--etc/
      |   `--init.d/
      |       `-- rcS
      `--tmp/

Modifying the ROMFS Image. The contents of the `/etc` directory are retained in the file `apps/nshlib/nsh_romfsimg.h` OR, if `CONFIG_NSH_ARCHROMFS` is defined, `include/arch/board/rcs.template`). In order to modify the start-up behavior, there are three things to study:

1. Configuration Options. The additional `CONFIG_NSH_ROMFSETC` configuration options discussed with the other NSH-specific configuration settings.

2. `tools/mkromfsimg.sh` Script. The script `tools/mkromfsimg.sh` creates `nsh_romfsimg.h`. It is not automatically executed. If you want to change the configuration settings associated with creating and mounting the `/tmp` directory, then it will be necessary to re-generate this header file using the `tools/mkromfsimg.sh` script.

   The behavior of this script depends upon three things:

   - The configuration settings then installed configuration.
   - The `genromfs` tool (available from http://romfs.sourceforge.net).
   - The file `apps/nshlib/rcS.template` (OR, if `CONFIG_NSH_ARCHROMFS` is defined `include/arch/board/rcs.template`.
3. `rcS.template`. The file `apps/nshlib/rcS.template` contains the general form of the `rcS` file; configured values are plugged into this template file to produce the final `rcS` file.

NOTE: `apps/nshlib/rcS.template` generates the standard, default `nsh_romfsimg.h` file. If `CONFIG_NSH_ARCHROMFS` is defined in the NuttX configuration file, then a custom, board-specific `nsh_romfsimg.h` file residing in `configs/<board>/include` will be used. NOTE when the OS is configured, `include/arch/board` will be linked to `configs/<board>/include`.

All of the startup-behavior is contained in `rcS.template`. The role of `mkromfsimg.sh` is to (1) apply the specific configuration settings to `rcS.template` to create the final `rcS`, and (2) to generate the header file `nsh_romfsimg.h` containg the ROMFS file system image.

# 2.0 Commands

## 2.1 Evaluate Expression (test)

Command Syntax:

    [ <expression> ]
    test <expression>

Synopsis. These are two alternative forms of the same command. They support evaluation of a boolean expression which sets `$?`. This command is used most frequently as the conditional command following the `if` in the `if-then[-else]-fi` construct.

Expression Syntax:

expression = simple-expression | !expression | expression -o expression | expression -a expression

simple-expression = unary-expression | binary-expression

unary-expression = string-unary | file-unary

string-unary = -n string | -z string

file-unary = -b file | -c file | -d file | -e file | -f file | -r file | -s file | -w file

binary-expression = string-binary | numeric-binary

string-binary = string = string | string == string | string != string

numeric-binary = integer -eq integer | integer -ge integer | integer -gt integer | integer -le integer | integer -lt integer | integer -ne integer

## 2.2 Concatenate Files (cat)

Command Syntax:

    cat <path> [<path> [<path> ...]]

Synopsis. This command copies and concatentates all of the files at `<path>` to the console (or to another file if the output is redirected).

## 2.3 Change Current Working Directory (cd)

Command Syntax:

    cd [<dir-path>|-|~|..]

Synopsis. Changes the current working directory (`PWD`). Also sets the previous working directory environment variable (`OLDPWD`).

Forms:

`cd <dir-path>` | sets the current working directory to `<dir-path>`.
`cd -` | sets the current working directory to the previous working directory ($`OLDPWD`). Equivalent to `cd $OLDPWD`.
`cd` or `cd ~` | set the current working directory to the 'home' directory. The home directory can be configured by setting `CONFIG_LIB_HOMEDIR` in the configuration file. The default home directory is `/`.
`cd ..` | sets the current working directory to the parent directory.

## 2.4 Copy Files (cp)

Command Syntax:

    cp <source-path> <dest-path>

Synopsis. Copy of the contents of the file at `<source-path>` to the location in the filesystem indicated by `<dest-path>`.

## 2.5 Show or set the date and time (date)

Command Syntax:

    date [-s "MMM DD HH:MM:SS YYYY"]

Synopsis. Show or set the current date and time. This command is only supported if the platform supported RTC hardware (`CONFIG_RTC=y`).

Only one format is used both on display and when setting the date/time: `MMM DD HH:MM:SS YYYY`. For example,

    data -s "Sep 1 11:30:00 2011"

24-hour time is used.

## 2.6 Copy and Convert Files (dd)

Command Syntax:

    dd if=<infile> of=<outfile> [bs=<sectsize>] [count=<sectors>] [skip=<sectors>]

Synopsis. Copy blocks from <infile> to <outfile>. <infile> or <outfile> may be the path to a standard file, a character device, or a block device. Examples follow:

1. Read from character device, write to regular file. This will create a new file of the specified size filled with zero.

       nsh> ls -l /dev
       /dev:
        crw-rw-rw-       0 zero
       nsh> dd if=/dev/zero of=/tmp/zeros bs=64 count=16
       nsh> ls -l /tmp
       /tmp:
        -rw-rw-rw-    1024 ZEROS

2. Read from character device, write to block device. This will fill the entire block device with zeros.

    nsh> ls -l /dev
    /dev:
     brw-rw-rw-       0 ram0
     crw-rw-rw-       0 zero
    nsh> dd if=/dev/zero of=/dev/ram0

3. Read from a block devic, write to a character device. This will read the entire block device and dump the contents in the bit bucket.

    nsh> ls -l /dev
    /dev:
     crw-rw-rw-       0 null
     brw-rw-rw-       0 ram0
    nsh> dd if=/dev/ram0 of=/dev/null

## 2.7 Echo Strings and Variables (echo)

Command Syntax:

    echo [<string|$name> [<string|$name>...]]

Synopsis. Copy the sequence of strings and expanded environment variables to console output (or to a file if the output is re-directed).

## 2.8 Execute User Code (exec)

Command Syntax:

    exec <hex-address>

Synopsis. Execute the user logic at address `<hex-address>`. NSH will pause until the execution unless the user logic is executed in background via `exec <hex-address> &`.

## 2.9 Exit NSH (exit)

Command Syntax:

    exit

Synopsis. Exit NSH. Only useful for the serial front end if you have started some other tasks (perhaps using the `exec` command) and you would like to have NSH out of the way. For the telnet front-end, `exit` terminates the telenet session.

## 2.10 Show Memory Manager Status (free)

Command Syntax:

    free

Synopsis. Show the current state of the memory allocator. For example,

    nsh> free
                 total       used       free    largest
    Mem:       4194288    1591552    2602736    2601584
    nsh>

Where:

`total` | This is the total size of memory allocated for use by malloc in bytes.
`used` | This is the total size of memory occupied by chunks handed out by malloc.
`free` | This is the total size of memory occupied by free (not in use) chunks.
`largest` | Size of the largest free (not in use) chunk.

## 2.11 Get File Via TFTP (get)

Command Syntax:

    get [-b|-n] [-f <local-path>] -h <ip-address> <remote-path>

Synopsis. Copy the file at `<remote-address>` from the host whose IP address is identified by `<ip-address>`.

Other options:

`-f <local-path>` | The file will be saved relative to the current working directory unless `<local-path>` is provided.
`-b|-n` | Selects either binary ("octect") or test ("netascii") transfer mode. Default: text.

## 2.12 Show Usage Command Usage (help)

Command Syntax:

    help

Synopsis. Presents summary information about each command to console.

## 2.13 Show Network Configuration (ifconfig)

Command Syntax:

    ifconfig [nic_name [ip_address]]

Synopsis. Two forms of the `ifconfig`command are supported:

1. With one or no arguments, `ifconfig` will shows the current configuration of the network and, perhaps, the status of ethernet device:

       ifconfig
       ifconfig [nic_name]

   As an example:

       nsh> ifconfig
       eth0    HWaddr 00:18:11:80:10:06
               IPaddr:10.0.0.2 DRaddr:10.0.0.1 Mask:255.255.255.0

   If uIP statistics are enabled (`CONFIG_NET_STATISTICS`), then this command will also show the detailed state of uIP.

2. If both the network interface name and an IP address are supplied as arguments, then `ifconfig` will set the address of the ethernet device:

       ifconfig nic_name ip_address

## 2.14 Send a signal to a task (kill)

    kill -<signal> <pid>

Synopsis. Send the <signal> to the task identified by <pid>.

    nsh> mkfifo /dev/fifo
    nsh> cat /dev/fifo &
    cat [2:128]
    nsh> ps
    PID   PRI SCHD TYPE   NP STATE    NAME
        0   0 FIFO TASK      READY    Idle Task()
        1 128 FIFO TASK      RUNNING  init()
        2 128 FIFO PTHREAD   WAITSEM  (51ea50)
    nsh> kill -9 2
    nsh: cat: open failed: 4
    nsh> ps
    PID   PRI SCHD TYPE   NP STATE    NAME
        0   0 FIFO TASK      READY    Idle Task()
        1 128 FIFO TASK      RUNNING  init()
    nsh>

NOTE: NuttX does not support a FULL POSIX signalling system. Standard signals like SIGCHLD, SIGINTR, SIGKILL, etc. do not exist in NuttX and sending those signal may not have the result that you expect. Rather, NuttX supports only what are referred to as POSIX real-time signals. These signals may be used to communicate with running tasks, may be use to waiting waiting tasks, etc. But, as an example, `kill -9` (SIGKILL) will not terminate a task.

## 2.15 Setup/teardown the Loop Device (losetup)

Command Syntax 1:

    losetup [-o ] [-r] <dev-path> <file-path>

Synopsis. Setup the loop device at <dev-path> to access the file at <file-path> as a block device. In the following example a 256Kb file is created (`dd`) and `losetup` is used to make the file accessible as a block device. A FAT file system is created (`mkfatfs`) and mounted (`mount`). Files can then be managed on the loop-mounted file.

    nsh> dd if=/dev/zero of=/tmp/image bs=512 count=512
    nsh> ls -l /tmp
    /tmp:
     -rw-rw-rw-   262144 IMAGE
    nsh> losetup /dev/loop0 /tmp/image
    nsh> ls -l /dev
    /dev:
     brw-rw-rw-       0 loop0
    nsh> mkfatfs /dev/loop0
    nsh> mount -t vfat /dev/loop0 /mnt/example
    nsh> ls -l /mnt
    ls -l /mnt
    /mnt:
     drw-rw-rw-       0 example/
    nsh> echo "This is a test" >/mnt/example/atest.txt
    nsh> ls -l /mnt/example
    /mnt/example:
     -rw-rw-rw-      16 ATEST.TXT
    nsh> cat /mnt/example/atest.txt
    This is a test
    nsh>

Command Syntax 2:

    losetup d <dev-path>

Synopsis. Teardown the setup for the loop device at <dev-path>.

## 2.16 List Directory Contents (ls)

Command Syntax:

    ls [-lRs] <dir-path>

Synopsis. Show the contents of the directory at `<dir-path>`. NOTE: `<dir-path>` must refer to a directory and no other filesystem object.

Options:

`-R` | Show the constents of specified directory and all of its sub-directories.
`-s` | Show the size of the files along with the filenames in the listing
`-l` | Show size and mode information along with the filenames in the listing.

## 2.17 Access Memory (mb, mh, and mw)

Command Syntax:

    mb <hex-address>[=<hex-value>][ <hex-byte-count>]
    mh <hex-address>[=<hex-value>][ <hex-byte-count>]
    mw <hex-address>[=<hex-value>][ <hex-byte-count>]

Synopsis. Access memory using byte size access (mb), 16-bit accesses (mh), or 32-bit access (mw). In each case,

`<hex-address>`. | Specifies the address to be accessed. The current value at that address will always be read and displayed.
`<hex-address>=<hex-value>`. | Read the value, then write `<hex-value>` to the location.
`<hex-byte-count>`. | Perform the mb, mh, or mw operation on a total of `<hex-byte-count>` bytes, increment the `<hex-address>` appropriately after each access

Example:

    nsh> mh 0 16
      0 = 0x0c1e
      2 = 0x0100
      4 = 0x0c1e
      6 = 0x0110
      8 = 0x0c1e
      a = 0x0120
      c = 0x0c1e
      e = 0x0130
      10 = 0x0c1e
      12 = 0x0140
      14 = 0x0c1e
    nsh>

## 2.18 Show Current Tasks and Threads (ps)

Command Syntax:

    ps

Synopsis. Show the currently active threads and tasks. For example,

    nsh> ps
    PID   PRI SCHD TYPE   NP STATE    NAME
        0   0 FIFO TASK      READY    Idle Task()
        1 128 RR   TASK      RUNNING  init()
        2 128 FIFO TASK      WAITSEM  nsh_telnetmain()
        3 100 RR   PTHREAD   WAITSEM  <pthread>(21)
    nsh>

## 2.19 Create a Directory (mkdir)

Command Syntax:

    mkdir <path>

Synopsis. Create the directory at `<path>`. All components of of `<path>` except the final directory name must exist on a mounted file system; the final directory must not.

Limited to Mounted File Systems. Recall that NuttX uses a pseudo filesystem for its root file system. The `mkdir` command can only be used to create directories in volumes set up with the `mount` command; it cannot be used to create directories in the pseudo filesystem.

Example:

    nsh> mkdir /mnt/fs/tmp
    nsh> ls -l /mnt/fs
    /mnt/fs:
     drw-rw-rw-       0 TESTDIR/
     drw-rw-rw-       0 TMP/
    nsh>

## 2.20 Create a FAT Filesystem (mkfatfs)

Command Syntax:

    mkfatfs <path>

Synopsis. Format a fat file system on the block device specified by `<path>`. NSH provides this command to access the `mkfatfs()` NuttX API. This block device must reside in the NuttX pseudo filesystem and must have been created by some call to `register_blockdriver()` (see `include/nuttx/fs.h`).

## 2.21 Create a FIFO (mkfifo)

Command Syntax:

    mkfifo <path>

Synopsis. Creates a FIFO character device anywhere in the pseudo file system, creating whatever psuedo directories that may be needed to complete the `<path>`. By convention, however, device drivers are place in the standard `/dev` directory. After it is created, the FIFO device may be used as any other device driver. NSH provides this command to access the `mkfifo()` NuttX API.

Example

    nsh> ls -l /dev
    /dev:
     crw-rw-rw-       0 console
     crw-rw-rw-       0 null
     brw-rw-rw-       0 ram0
    nsh> mkfifo /dev/fifo
    nsh> ls -l /dev
    ls -l /dev
    /dev:
     crw-rw-rw-       0 console
     crw-rw-rw-       0 fifo
     crw-rw-rw-       0 null
     brw-rw-rw-       0 ram0
    nsh>

## 2.22 Create a RAMDISK (mkrd)

Command Syntax:

    mkrd [-m <minor>] [-s <sector-size>] <nsectors>

Synopsis. Create a ramdisk consisting of `<nsectors>`, each of size `<sector-size>` (or 512 bytes if `<sector-size>` is not specified. The ramdisk will be registered as `/dev/ram<n>` (if `<n>` is not specified, mkrd will attempt to register the ramdisk as `/dev/ram0`.

Example

    nsh> ls /dev
    /dev:
     console
     null
     ttyS0
     ttyS1
    nsh> mkrd 1024
    nsh> ls /dev
    /dev:
     console
     null
     ram0
     ttyS0
     ttyS1
    nsh>

Once the ramdisk has been created, it may be formatted using the `mkfatfs` command and mounted using the `mount` command.

Example

    nsh> mkrd 1024
    nsh> mkfatfs /dev/ram0
    nsh> mount -t vfat /dev/ram0 /tmp
    nsh> ls /tmp
    /tmp:
    nsh>

## 2.23 Mount a File System (mount)

Command Syntax:

    mount -t <fstype> <block-device> <dir-path>

Synopsis. The 'm ount' command mounts a file system in the NuttX psuedo filesystem. 'mount' performs a three way associating, binding:

1. File system. The '-t `<fstype>`' option identifies the type of file system that has been formatted on the `<block-device>`. As of this writing, `vfat` is the only supported value for `<fstype>`
2. Block Device. The `<block-device>` argument is the full or relative path to a block driver inode in the pseudo filesystem. By convention, this is a name under the `/dev` sub-directory. This `<block-device>` must have been previously formatted with the same file system type as specified by `<fstype>`
3. Mount Point. The mount point, `<dir-path>`, is the location in the pseudo filesystem where the mounted volume will appear. This mount point can only reside in the NuttX pseudo filesystem. By convention, this mount point is a subdirectory under `/mnt`. The mount command will create whatever psuedo directories that may be needed to complete the full path but the full path must not already exist.

After the volume has been mounted in the NuttX pseudo filesystem, it may be access in the same way as other objects in thefile system.

Example

    nsh> ls -l /dev
    /dev:
     crw-rw-rw-       0 console
     crw-rw-rw-       0 null
     brw-rw-rw-       0 ram0
    nsh> ls /mnt
    nsh: ls: no such directory: /mnt
    nsh> mount -t vfat /dev/ram0 /mnt/fs
    nsh> ls -l /mnt/fs/testdir
    /mnt/fs/testdir:
     -rw-rw-rw-      15 TESTFILE.TXT
    nsh> echo "This is a test" >/mnt/fs/testdir/example.txt
    nsh> ls -l /mnt/fs/testdir
    /mnt/fs/testdir:
    -rw-rw-rw-      15 TESTFILE.TXT
     -rw-rw-rw-      16 EXAMPLE.TXT
    nsh> cat /mnt/fs/testdir/example.txt
    This is a test
    nsh>

## 2.24 Check Network Peer (ping)

Command Syntax:

    ping [-c <count>] [-i <interval>] <ip-address>

Synopsis. Test the network communication with a remote peer. Example,

    nsh> ping 10.0.0.1
    PING 10.0.0.1 56 bytes of data
    56 bytes from 10.0.0.1: icmp_seq=1 time=0 ms
    56 bytes from 10.0.0.1: icmp_seq=2 time=0 ms
    56 bytes from 10.0.0.1: icmp_seq=3 time=0 ms
    56 bytes from 10.0.0.1: icmp_seq=4 time=0 ms
    56 bytes from 10.0.0.1: icmp_seq=5 time=0 ms
    56 bytes from 10.0.0.1: icmp_seq=6 time=0 ms
    56 bytes from 10.0.0.1: icmp_seq=7 time=0 ms
    56 bytes from 10.0.0.1: icmp_seq=8 time=0 ms
    56 bytes from 10.0.0.1: icmp_seq=9 time=0 ms
    56 bytes from 10.0.0.1: icmp_seq=10 time=0 ms
    10 packets transmitted, 10 received, 0% packet loss, time 10190 ms
    nsh>

## 2.25 Send File Via TFTP (put)

Command Syntax:

    put [-b|-n] [-f <remote-path>] -h <ip-address> <local-path>

Synopsis. Copy the file at `<local-address>` to the host whose IP address is identified by `<ip-address>`.

Other options:

`-f <remote-path>` | The file will be saved relative with the same name on the host unless `<remote-path>` is provided.
`-b|-n` | Selects either binary ("octect") or test ("netascii") transfer mode. Default: text.

## 2.26 Show Current Working Directory (pwd)

Command Syntax:

    pwd

Synopsis. Show the current working directory.

    nsh> cd /dev
    nsh> pwd
    /dev
    nsh>

Same as `echo $PWD`.

    nsh> echo $PWD
    /dev
    nsh>

## 2.27 Remove a File (rm)

Command Syntax:

    rm <file-path>

Synopsis. Remove the specified `<file-path>` name from the mounted file system. Recall that NuttX uses a pseudo filesystem for its root file system. The `rm` command can only be used to remove (unlink) files in volumes set up with the `mount` command; it cannot be used to remove names in the pseudo filesystem.

Example:

    nsh> ls /mnt/fs/testdir
    /mnt/fs/testdir:
     TESTFILE.TXT
     EXAMPLE.TXT
    nsh> rm /mnt/fs/testdir/example.txt
    nsh> ls /mnt/fs/testdir
    /mnt/fs/testdir:
     TESTFILE.TXT
    nsh>

## 2.28 Remove a Directory (rmdir)

Command Syntax:

    rmdir <dir-path>

Synopsis. Remove the specified `<dir-path>` directory from the mounted file system. Recall that NuttX uses a pseudo filesystem for its root file system. The `rmdir` command can only be used to remove directories from volumes set up with the `mount` command; it cannot be used to remove directories from the pseudo filesystem.

Example:

    nsh> mkdir /mnt/fs/tmp
    nsh> ls -l /mnt/fs
    /mnt/fs:
     drw-rw-rw-       0 TESTDIR/
     drw-rw-rw-       0 TMP/
    nsh> rmdir /mnt/fs/tmp
    nsh> ls -l /mnt/fs
    /mnt/fs:
     drw-rw-rw-       0 TESTDIR/
    nsh>

## 2.29 Set an Environment Variable (set)

Command Syntax:

    set <name> <value>

Synopsis. Set the environment variable `<name>` to the string `<value>`. For example,

    nsh> echo $foobar

    nsh> set foobar foovalue
    nsh> echo $foobar
    foovalue
    nsh>

## 2.30 Execute an NSH Script (sh)

Command Syntax:

    sh <script-path>

Synopsis. Execute the sequence of NSH commands in the file referred to by `<script-path>`.

## 2.31 Wait for Seconds (sleep)

Command Syntax:

    sleep <sec>

Synopsis. Pause execution (sleep) for `<sec>` seconds.

## 2.32 Unmount a File System (umount)

Command Syntax:

    umount <dir-path>

Synopsis. Un-mount the file system at mount point `<dir-path>`. The `umount` command can only be used to un-mount volumes previously mounted using `mount` command.

Example:

    nsh> ls /mnt/fs
    /mnt/fs:
     TESTDIR/
    nsh> umount /mnt/fs
    nsh> ls /mnt/fs
    /mnt/fs:
    nsh: ls: no such directory: /mnt/fs
    nsh>

## 2.33 Unset an Environment Variable (unset)

Command Syntax:

    unset <name>

Synopsis. Remove the value associated with the environment variable `<name>`. Example:

    nsh> echo $foobar
    foovalue
    nsh> unset foobar
    nsh> echo $foobar

    nsh>

## 2.34 Wait for Microseconds (usleep)

Command Syntax:

    usleep <usec>

Synopsis. Pause execution (sleep) of `<usec>` microseconds.

2.35 Get File Via HTTP (wget)

Command Syntax:

    wget [-o <local-path>] <url>

Synopsis. Use HTTP to copy the file at `<url>` to the current directory.

Options:

`-o <local-path>` | The file will be saved relative to the current working directory and with the same name as on the HTTP server unless `<local-path>` is provided.

## 2.36 Hexadecimal dump (xd)

Command Syntax:

    xd <hex-address> <byte-count>

Synopsis. Dump `<byte-count>` bytes of data from address `<hex-address>`.

Example:

    nsh> xd 410e0 512
    Hex dump:
    0000: 00 00 00 00 9c 9d 03 00 00 00 00 01 11 01 10 06 ................
    0010: 12 01 11 01 25 08 13 0b 03 08 1b 08 00 00 02 24 ....%..........$
    ...
    01f0: 08 3a 0b 3b 0b 49 13 00 00 04 13 01 01 13 03 08 .:.;.I..........
    nsh>

# 3.0 Configuration Settings

The availability of the above commands depends upon features that may or may not be enabled in the NuttX configuration file. The following table indicates the dependency of each command on NuttX configuration settings. General configuration settings are discussed in the NuttX Porting Guide. Configuration settings specific to NSH as discussed at the bottom of this document.

Note that in addition to general NuttX configuation settings, each NSH command can be individually disabled via the settings in the rightmost column. All of these settings make the configuration of NSH potentially complex but also allow it to squeeze into very small memory footprints.

## 3.1 Command Dependencies on Configuration Settings

Table. Command Dependencies on Configuration Settings

Command | Depends on Configuration | Can Be Disabled with
--- | --- | ---
`[` | !`CONFIG_NSH_DISABLESCRIPT` | `CONFIG_NSH_DISABLE_TEST`
`cat` | `CONFIG_NFILE_DESCRIPTORS` > 0 | `CONFIG_NSH_DISABLE_CAT`
`cd` | !`CONFIG_DISABLE_ENVIRON` && `CONFIG_NFILE_DESCRIPTORS` > 0 | `CONFIG_NSH_DISABLE_CD`
`cp` | `CONFIG_NFILE_DESCRIPTORS` > 0 | `CONFIG_NSH_DISABLE_CP`
`date` | !`CONFIG_DISABLE_CLOCK` && `CONFIG_RTC` | `CONFIG_NSH_DISABLE_DATE`
`dd` | `CONFIG_NFILE_DESCRIPTORS` > 0 | `CONFIG_NSH_DISABLE_DD`
`echo` |  | `CONFIG_NSH_DISABLE_ECHO`
`exec` |  | `CONFIG_NSH_DISABLE_EXEC`
`exit` |  | `CONFIG_NSH_DISABLE_EXIT`
`free` |  | `CONFIG_NSH_DISABLE_FREE`
`get` | `CONFIG_NET` && `CONFIG_NET_UDP` && `CONFIG_NFILE_DESCRIPTORS` > 0 && `CONFIG_NET_BUFSIZE` >= 5581 | `CONFIG_NSH_DISABLE_GET`
`help` |  | `CONFIG_NSH_DISABLE_HELP`
`ifconfig` | `CONFIG_NET` | `CONFIG_NSH_DISABLE_IFCONFIG`
`kill` | !`CONFIG_DISABLE_SIGNALS` | `CONFIG_NSH_DISABLE_KILL`
`losetup` | !`CONFIG_DISABLE_MOUNTPOINT` && `CONFIG_NFILE_DESCRIPTORS` > 0 | `CONFIG_NSH_DISABLE_LOSETUP`
`ls` | `CONFIG_NFILE_DESCRIPTORS` > 0 | `CONFIG_NSH_DISABLE_LS`
`mb,mh,mw` |  | `CONFIG_NSH_DISABLE_MB`, `CONFIG_NSH_DISABLE_MH`, `CONFIG_NSH_DISABLE_MW`
`mkdir` | !`CONFIG_DISABLE_MOUNTPOINT` && `CONFIG_NFILE_DESCRIPTORS` > 0 && `CONFIG_FS_WRITABLE`4 | `CONFIG_NSH_DISABLE_MKDIR`
`mkfatfs` | !`CONFIG_DISABLE_MOUNTPOINT` && `CONFIG_NFILE_DESCRIPTORS` > 0 && `CONFIG_FS_FAT` | `CONFIG_NSH_DISABLE_MKFATFS`
`mkfifo` | `CONFIG_NFILE_DESCRIPTORS` > 0 | `CONFIG_NSH_DISABLE_MKFIFO`
`mkrd` | !`CONFIG_DISABLE_MOUNTPOINT` && `CONFIG_NFILE_DESCRIPTORS` > 0 && `CONFIG_FS_WRITABLE`4 | `CONFIG_NSH_DISABLE_MKRD`
`mount` | !`CONFIG_DISABLE_MOUNTPOINT` && `CONFIG_NFILE_DESCRIPTORS` > 0 && `CONFIG_FS_READABLE`3 | `CONFIG_NSH_DISABLE_MOUNT`
`ping` | `CONFIG_NET` && `CONFIG_NET_ICMP` && `CONFIG_NET_ICMP_PING` && !`CONFIG_DISABLE_CLOCK` && !`CONFIG_DISABLE_SIGNALS` | `CONFIG_NSH_DISABLE_PING`
`ps` |  | `CONFIG_NSH_DISABLE_PS`
`put` | `CONFIG_NET` && `CONFIG_NET_UDP` && `CONFIG_NFILE_DESCRIPTORS` > 0 && `CONFIG_NET_BUFSIZE` >= 5581,2 | `CONFIG_NSH_DISABLE_PUT`
`pwd` | !`CONFIG_DISABLE_ENVIRON` && `CONFIG_NFILE_DESCRIPTORS` > 0 | `CONFIG_NSH_DISABLE_PWD`
`rm` | !`CONFIG_DISABLE_MOUNTPOINT` && `CONFIG_NFILE_DESCRIPTORS` > 0 && `CONFIG_FS_WRITABLE`4 | `CONFIG_NSH_DISABLE_RM`
`rmdir` | !`CONFIG_DISABLE_MOUNTPOINT` && `CONFIG_NFILE_DESCRIPTORS` > 0 && `CONFIG_FS_WRITABLE`4 | `CONFIG_NSH_DISABLE_RMDIR`
`set` | !`CONFIG_DISABLE_ENVIRON` | `CONFIG_NSH_DISABLE_SET`
`sh` | `CONFIG_NFILE_DESCRIPTORS > 0 && `CONFIG_NFILE_STREAMS > 0 && !`CONFIG_NSH_DISABLESCRIPT``` | `CONFIG_NSH_DISABLE_SH`
`sleep` | !`CONFIG_DISABLE_SIGNALS` | `CONFIG_NSH_DISABLE_SLEEP`
`test` | !`CONFIG_NSH_DISABLESCRIPT` | `CONFIG_NSH_DISABLE_TEST`
`umount` | !`CONFIG_DISABLE_MOUNTPOINT` && `CONFIG_NFILE_DESCRIPTORS` > 0 && `CONFIG_FS_READABLE`3 | `CONFIG_NSH_DISABLE_UMOUNT`
`unset` | !`CONFIG_DISABLE_ENVIRON` | `CONFIG_NSH_DISABLE_UNSET`
`usleep` | !`CONFIG_DISABLE_SIGNALS` | `CONFIG_NSH_DISABLE_USLEEP`
`wget` | `CONFIG_NET` && `CONFIG_NET_TCP` && `CONFIG_NFILE_DESCRIPTORS` > 0 | `CONFIG_NSH_DISABLE_WGET`
`xd` |  | `CONFIG_NSH_DISABLE_XD`

1 Because of hardware padding, the actual required packet size may be larger
2 Special TFTP server start-up optionss will probably be required to permit creation of files for the correct operation of the `put` command.
3 `CONFIG_FS_READABLE` is not a user configuration but is set automatically if any readable filesystem is selected. At present, this is either `CONFIG_FS_FAT` or `CONFIG_FS_ROMFS`.
4 `CONFIG_FS_WRITABLE` is not a user configuration but is set automatically if any writable filesystem is selected. At present, this is only `CONFIG_FS_FAT`.

## 3.2 NSH-Specific Configuration Settings

The behavior of NSH can be modified with the following settings in the `configs/<board-name>/defconfig` file:

Configuration | Description
--- | ---
`CONFIG_NSH_BUILTIN_APPS` | Support external registered, "named" applications that can be executed from the NSH command line (see apps/README.txt for more information).
`CONFIG_NSH_FILEIOSIZE` | Size of a static I/O buffer used for file access (ignored if there is no filesystem). Default is 1024.
`CONFIG_NSH_STRERROR` | strerror(errno) makes more readable output but strerror() is very large and will not be used unless this setting is y
`CONFIG_NSH_LINELEN` | The maximum length of one command line and of one output line. Default: 80
`CONFIG_NSH_STACKSIZE` | The stack size to use when spawning new threads or tasks. Such new threads are generated when a command is executed in background or as new TELNET connections are established.
`CONFIG_NSH_NESTDEPTH` | The maximum number of nested `if-then[-else]-fi` sequences that are permissable. Default: 3
`CONFIG_NSH_DISABLESCRIPT` | This can be set to y to suppress support for scripting. This setting disables the `sh`, `test`, and `[` commands and the `if-then[-else]-fi` construct. This would only be set on systems where a minimal footprint is a necessity and scripting is not.
`CONFIG_NSH_DISABLEBG` | This can be set to y to suppress support for background commands. This setting disables the `nice` command prefix and the `&` command suffix. This would only be set on systems where a minimal footprint is a necessity and background command execution is not.
`CONFIG_NSH_MMCSDMINOR` | If the architecture supports an MMC/SD slot and if the NSH architecture specific logic is present, this option will provide the MMC/SD minor number, i.e., the MMC/SD block driver will be registered as `/dev/mmcsd` N where N is the minor number. Default is zero.
`CONFIG_NSH_ROMFSETC` | Mount a ROMFS filesystem at `/etc` and provide a startup script at `/etc/init.d/rcS`. The default startup script will mount a FAT FS RAMDISK at `/tmp` but the logic is easily extensible.
`CONFIG_NSH_CONSOLE` | If `CONFIG_NSH_CONSOLE` is set to y, then a serial console front-end is selected.
`CONFIG_NSH_CONDEV` | If `CONFIG_NSH_CONSOLE` is set to y, then `CONFIG_NSH_CONDEV` may also be set to select the serial device used to support the NSH console. This should be set to the quoted name of a readable/write-able character driver such as: `CONFIG_NSH_CONDEV="/dev/ttyS1"`. This is useful, for example, to separate the NSH command line from the system console when the system console is used to provide debug output. Default: `stdin` and `stdout` (probably "`/dev/console`") NOTE: When any other device other than `/dev/console` is used for a user interface, (1) linefeeds (`\n`) will not be expanded to carriage return / linefeeds (`\r\n`). You will need to configure your terminal program to account for this. And (2) input is not automatically echoed so you will have to turn local echo on.
`CONFIG_NSH_TELNET` | If `CONFIG_NSH_TELNET` is set to y, then a TELENET server front-end is selected. When this option is provided, you may log into NuttX remotely using telnet in order to access NSH.
`CONFIG_NSH_ARCHINIT` | Set `CONFIG_NSH_ARCHINIT` if your board provides architecture specific initialization via the board-specific function `nsh_archinitialize()`. This function will be called early in NSH initialization to allow board logic to do such things as configure MMC/SD slots.

One or both of `CONFIG_NSH_CONSOLE` and `CONFIG_NSH_TELNET` must be defined. If `CONFIG_NSH_TELNET` is selected, then there some other configuration settings that apply:

Configuration | Description
--- | ---
`CONFIG_NET=y` | Of course, networking must be enabled.
`CONFIG_NSOCKET_DESCRIPTORS` | And, of course, you must allocate some socket descriptors.
`CONFIG_NET_TCP=y` | TCP/IP support is required for telnet (as well as various other TCP-related configuration settings).
| `CONFIG_NSH_IOBUFFER_SIZE` | Determines the size of the I/O buffer to use for sending/ receiving TELNET commands/reponses
`CONFIG_NSH_DHCPC` | Obtain the IP address via DHCP.
`CONFIG_NSH_IPADDR` | If `CONFIG_NSH_DHCPC` is NOT set, then the static IP address must be provided.
`CONFIG_NSH_DRIPADDR` | Default router IP address
`CONFIG_NSH_NETMASK` | Network mask
`CONFIG_NSH_NOMAC` | Set if your ethernet hardware has no built-in MAC address. If set, a bogus MAC will be assigned.

If you use DHCPC, then some special configuration network options are required. These include:

Configuration | Description
--- | ---
`CONFIG_NET=y` | Of course, networking must be enabled.
`CONFIG_NSOCKET_DESCRIPTORS` | And, of course, you must allocate some socket descriptors.
`CONFIG_NET_UDP=y` | UDP support is required for DHCP (as well as various other UDP-related configuration settings).
`CONFIG_NET_BROADCAST=y` | UDP broadcast support is needed.
`CONFIG_NET_BUFSIZE=650` (or larger) | Per RFC2131 (p. 9), the DHCP client must be prepared to receive DHCP messages of up to 576 bytes (excluding Ethernet, IP, or UDP headers and FCS).

If `CONFIG_NSH_ROMFSETC` is selected, then the following additional configuration setting apply:

Configuration | Description
--- | ---
`CONFIG_NSH_ARCHROMFS` | May be defined to specify an alternative ROMFS image that can be found at `configs/<board>/include/nsh_romfsimg.h`.
`CONFIG_NSH_ROMFSMOUNTPT` | The default mountpoint for the ROMFS volume is `"/etc"`, but that can be changed with this setting. This must be a absolute path beginning with '`/`' and enclosed in quotes.
`CONFIG_NSH_INITSCRIPT` | This is the relative path to the startup script within the mountpoint. The default is `"init.d/rcS"`. This is a relative path and must not start with '`/`' but must be enclosed in quotes.
`CONFIG_NSH_ROMFSDEVNO` | This is the minor number of the ROMFS block device. The default is '`0`' corresponding to `/dev/ram0`.
`CONFIG_NSH_ROMFSSECTSIZE` | This is the sector size to use with the ROMFS volume. Since the default volume is very small, this defaults to 64 but should be increased if the ROMFS volume were to be become large. Any value selected must be a power of 2.

When the default `rcS` file used when `CONFIG_NSH_ROMFSETC` is selected, it will mount a FAT FS under `/tmp`. The following selections describe that FAT FS.

Configuration | Description
--- | ---
`CONFIG_NSH_FATDEVNO` | This is the minor number of the FAT FS block device. The default is '`1`' corresponding to `/dev/ram1`.
`CONFIG_NSH_FATSECTSIZE` | This is the sector size use with the FAT FS. Default is 512.

# Index

`$?` `[` Background commands Background command priority Built-in variables `cat` `cd` Command summaries Conditional command execution `CONFIG_NSH_CONSOLE` `CONFIG_NSH_DHCPC` `CONFIG_NSH_DISABLEBG` `CONFIG_NSH_DISABLESCRIPT` `CONFIG_NSH_DRIPADDR` `CONFIG_NSH_FATDEVNO` `CONFIG_NSH_FATMOUNTPT` `CONFIG_NSH_FATNSECTORS` `CONFIG_NSH_FATSECTSIZE` `CONFIG_NSH_FILEIOSIZE` `CONFIG_NSH_INITSCRIPT` `CONFIG_NSH_IOBUFFER_SIZE` `CONFIG_NSH_IPADDR` `CONFIG_NSH_LINELEN` `CONFIG_NSH_NESTDEPTH` `CONFIG_NSH_NETMASK` `CONFIG_NSH_NOMAC` `CONFIG_NSH_ROMFSDEVNO` `CONFIG_NSH_ROMFSETC` `CONFIG_NSH_ARCHROMFS` `CONFIG_NSH_ROMFSMOUNTPT` `CONFIG_NSH_ROMFSSECTSIZE` `CONFIG_NSH_STACKSIZE` `CONFIG_NSH_STRERROR` `CONFIG_NSH_TELNET` Configuration settings Configuration settings, command dependencies Configuration settings, NSH-specific `cp` Current working directory `date` `dd` `echo` Environment Variables `/etc/init.d/rcS` | `exec` `exit` `free` `get` Greeting `help` `if-then[-else]-fi` `ifconfig` `kill` `losetup` ls `mb` `mh` `mw` `mkdir` `mkfatfs` `mkfifo` `mkrd` `mount` `nice` `OLDPWD` Overview `ping` Prompt `ps` `put` `pwd` `PWD` Re-directed commands `rm` `rmdir` `set` `sh` Simple commands `sleep` start-up script `test` `umount` `unset` `usleep` `wget` `xd`