User Tools

Site Tools


command

Command

Three kinds of commands:

  • shell built-ins
  • binary executables (compiled programs)
  • shell scripts

Builtins

The shell includes a number of builtin commands. Some of these commands are also referred to as keywords or reserved words.

$ help # list all builtin shell commands and keywords, roughly 80
bg
cd
exec
for
wait

External Commands

Some commands are binary executables or compiled programs. Each command is a binary file residing in either /sbin or /bin.

$ ls /sbin # list the system binary executables, roughly 300
addgroup
cron
deluser
shutdown
$ ls /bin # list the user binary executables, roughly 1400
kill
ls
top
tmux

Use type to learn the usage of a word in the shell.

$ type exec # show the type of a word as used in the shell
exec is a shell builtin
$ type for
for is a shell keyword
$ type ps
ps is /usr/bin/ps

Arguments

Commands take arguments, aka parameters. There are two types of arguments, options and operands. If the command is the verb, the operand is the object, and the options are the adverbs.

Options are identified with three different syntax variations: BSD, UNIX, GNU.

$ ps f # BSD style: no dash, one letter, no dash
$ ps -f # UNIX style: one dash, one letter
$ ps --forest # GNU long style: complete word, two dashes

Single letter options can be strung together without whitespace and without repeating the dash, like this:

$ ls -al # is same as ls -a -l

Exit Code

Most commands return an integer exit code, with 0 indicating normal completion.

Input and Output

Some commands take input from the keyboard and write output to the display.

Internally this is done with three file handles:

 0) stdin,  standard in, input from keyboard
 1) stdout, standard out, output to display
 2) stderr. standard error, output to display

Redirection

Each of these file handles can be redirected to a file.

<filename redirects stdin
>filename redirects stdout, overwriting filename
>>filename redirects stdout, appending to filename
&filename redirects stderr

Example

$ echo ‘this is my data’ >data.txt # redirect stdout to file data.txt 
$ echo ‘here is more data’ >>data.txt # redirect stdout, append to file data.txt 
$ echo ‘this is my data’ &errors.txt # redirect stderr to file errors.txt
$ echo ‘this is my data’ &>out.txt # redirect both stdout and stderr to out.txt
$ cat <errors.txt >log.txt # redirect stdin and stdout, copy errors.txt to log.txt

Pipe

Two programs can be chained together, redirecting the output from the first program to the input of the second program.

$ ps -e | grep chrom

That is equivalent to redirection with a temporary file

$ ps -e >temp.txt
$ grep chrom <temp.txt
$ del temp.txt

Conditional Execution

Reserved words, shell keywords

  • !
  • case
  • do
  • done
  • elif
  • else
  • esac
  • fi
  • for
  • function
  • if
  • in
  • select
  • then
  • until
  • while
  • {
  • }
  • time
  • *
command.txt · Last modified: 2021/01/28 05:46 by 127.0.0.1

Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain
Public Domain Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki