
nash Command in Linux
The nash command in Unix and Linux is a simple script interpreter designed to run scripts on an initial RAM disk (initrd) image. It is primarily used to execute simple Linuxrc scripts during the boot process. It runs simple scripts on an initrd image, which is an initial RAM disk used during the Linux boot process.
Arguments to commands can be enclosed in either single or double quotes to include spaces. Spaces outside of quotations delineate arguments, with backslash escaping supported.
If nash is invoked as modprobe, it will immediately exit with a return code of zero. This feature helps prevent extraneous kernel error messages during startup by allowing initrd to handle modules without unnecessary errors.
In addition, nash supports two types of commands, external Commands and built-in commands. Built-in commands can be executed directly while external commands are executed from the filesystem using execve(). If command names are given without a path, nash will search its built-in PATH: /usr/bin, /bin, /sbin, /usr/sbin.
Table of Contents
Here is a comprehensive guide to the options available with the nash command −
Syntax of nash Command
The following is the general syntax for the nash Command −
nash [--quiet] [--force] script
Where −
- [--quiet] − Suppresses output, making the execution quiet.
- [--force] − Forces the execution of the script, even if nash doesn’t appear to be running from an initrd image.
- script − The script file to be executed by nash.
Built-in Commands Supported by nash
nash supports a variety of built-in commands designed to perform tasks related to file management and script execution. Here are the available commands and their descriptions −
Commands | Description |
---|---|
access -[r][w][x][f] path | Checks whether the current user has sufficient permissions to read, write, or execute the specified path, or if the file exists. |
echo [item]* [> filename] | Prints the given text strings with spaces in between each item. The output can be optionally redirected to a file. |
exec <command> | Executes the specified command, replacing the nash process with the new command. |
find dir -name name | Displays the paths to files named name within or below the directory dir. This is a limited implementation of the find(1) command. |
losetup /dev/loopdev file | Associates the specified file with the loopback device /dev/loopdev |
mkdevices path | Creates device files for all block devices listed in /proc/partitions within the specified directory path |
mkdir [-p] path | Creates the directory path. If -p is specified, it does not raise an error if the directory already exists. This is a subset of the standard mkdir -p behavior. |
mknod path [c|b] major minor | Creates a device inode for path. This is similar to mkdev(1), except it does not create named pipes and automatically creates necessary directories in the path. |
mkdmnod | Creates a device inode for the device mapper control inode at /dev/mapper/control. It is not recreated if it already exists with the correct major/minor numbers. |
mkrootdev path |
Creates a block device inode for path that should be mounted as the root filesystem. It uses the device suggested by the root= kernel command line argument, or the device number from /proc/sys/kernel/real-root-dev if no root= argument is present. |
mount [--ro] -o opts -t type device mntpoint |
Mounts a filesystem. This command does not support NFS and must be used as specified. If device is of the form LABEL=foo, it searches /proc/partitions for the corresponding device. --ro mounts the filesystem as read-only. |
pivot_root newrootpath oldrootpath | Makes the filesystem mounted at newrootpath the new root filesystem and mounts the current root filesystem at oldrootpath. |
readlink path | Displays the target of the symbolic link path. |
raidautorun mddevice | Performs RAID autodetection on all RAID-typed partitions. The mddevice must be a RAID device. |
setquiet | Suppresses the display of any subsequent echo commands in the script. |
showlabels | Displays a table of devices along with their filesystem labels and UUIDs. |
sleep num | Pauses execution for num seconds. |
switchroot newrootpath | Makes the filesystem mounted at newrootpath the new root filesystem by moving the mount point. This works only in kernel versions 2.6 or later. |
umount path | Unmounts the filesystem mounted at path. |
Examples of nash Command in Linux
In this section, we'll explore some practical examples of the nash command using various built-in commands and options −
Access Command
To check if a user has read and write permissions for a file using nash, you can use the access command with the "-rw" flags.
nash -c "access -rw myfile,txt"
This command executes the script, checking if the current user has read and write permissions for the specified file.
Echo a Message to a File
To write the message "Hello, World!" to a file named output.txt using the nash command, you can use the following syntax −
nash -c "echo 'Hello, World!' > output.txt"
Exec Command
The exec command replaces the nash process with the specified command. This is particularly useful when executing another command within the nash environment.
To execute a command, overlaying the nash process, you can simply run −
nash -c "exec /bin/ls"
Upon running this command, nash executes /bin/ls and list the files and directories in the current working directory.
Find Command
If you want to find a file (e.g., example.txt) within the /home directory, you can use the find command like this −
find /home -name 'example.txt'
This command searches for the file example.txt starting from the /home directory and its subdirectories.
Mkdir Command
To create a directory, including any necessary parent directories that don't already exist, you can use the following command −
nash -c "mkdir -p mydirectory
Create a block device inode
To create a block device inode, you can use the following command −
nash -c "mknod /dev/example b 7 0"
This command creates a block device inode named example in the /dev directory with major number 7 and minor number 0.
Mount a Filesystem
To mount a filesystem with specific options, you can use the following syntax −
nash -c "mount -t ext4 -o defaults /dev/sda1 /mnt"
This command mounts the filesystem on /dev/sda1 to the /mnt directory using the ext4 filesystem type and default mount options.
Sleep Command
To pause the execution of a script for a specified number of seconds, you can use the sleep command −
nash -c "sleep 10"
This commandl pause the execution for 10 seconds.
Unmount a Filesytem
To unmount a filesystem, you can simply use the umount command −
nash -c "umount /mnt"
This command unmounts the filesystem at /mnt.
Running a Script
The following is script that includes all the commands we've mentioned above. Save this script to a file named example.nash −
#!/bin/bash # Access Command nash -c "access -rw myfile.txt" # Echo a Message to a File nash -c "echo 'Hello, World!' > output.txt" # Exec Command nash -c "exec /bin/ls" # Find Command nash -c "find /home -name 'example.txt'" # Mkdir Command nash -c "mkdir -p mydirectory" # Create a block device inode nash -c "mknod /dev/example b 7 0" # Mount Command nash -c "mount -t ext4 -o defaults /dev/sda1 /mnt" # Sleep Command nash -c "sleep 10" # Umount Command nash -c "umount /mnt"
To run the script, use the following command −
nash --quiet --force example.nash
Conclusion
nash is a robust and efficient script interpreter, ideal for executing scripts during the boot process in Unix and Linux systems. Its simplicity, combined with support for built-in and external commands, makes it a versatile tool for managing initial RAM disk operations.