The shell¶
The shell, /bin/sh, runs commands that you type. It runs in Mimir (the
terminal on the desktop) and on the console.
The code: src/user/sh/
The prompt shows the current folder:
skl:/home/user $
Edit a command¶
| Key | Result |
|---|---|
| Left, Right | move the cursor |
| Home or Ctrl+A, End or Ctrl+E | go to the start or the end of the line |
| Backspace, Delete | delete the character before or at the cursor |
| Ctrl+K | delete from the cursor to the end of the line |
| Ctrl+U | delete from the start of the line to the cursor |
| Ctrl+W | delete the word before the cursor |
| Ctrl+L | clear the screen |
| Ctrl+C | drop the line and show a new prompt |
| Ctrl+D | stop the shell (only on an empty line) |
| Up, Down | the commands before, from the history |
| Enter | run the command |
A line can be longer than the terminal is wide. It then continues on the next row.
The history keeps the last 128 commands. It does not keep a command two
times in a row. The history command shows the list.
Run commands¶
The shell finds a command in the folders of PATH. The default PATH
is /bin.
| You type | Result |
|---|---|
ls -l /bin |
runs ls with two arguments |
ls >files.txt |
writes the output to files.txt |
ls >>files.txt |
adds the output to the end of files.txt |
sort <files.txt |
reads the input from files.txt |
make 2>errors.txt |
writes the error output (descriptor 2) to errors.txt |
make >log.txt 2>&1 |
writes the output and the error output to log.txt |
echo failed >&2 |
writes to the error output |
sleep 60 & |
runs the command in the background |
cd /tmp; ls |
runs cd, then ls |
make && ./run |
runs ./run only if make succeeds (exit status 0) |
test -d out \|\| mkdir out |
runs mkdir only if test fails |
! grep -q x f && echo none |
! inverts the exit status of a pipeline |
Spaces around the operators are optional: ls|wc -l is the same as
ls | wc -l. A redirection applies to one command, and the shell does
them from left to right, so 2>&1 >log.txt sends errors to the terminal.
A pipeline sends the output of one command into the next one:
$ ls /bin | wc -l
& runs everything before it, back to the last ; or &, in the
background: sleep 1 && echo done & is one background job.
A line that ends with |, &&, || or \ continues on the next line.
The shell shows > while it waits for the rest. # starts a comment
that runs to the end of the line.
Quotes¶
"two words"is one word.$in double quotes expands;\$gives a plain$.'two words'is one word, and everything in it stays as it is:'$HOME'is the text$HOME.\before a character keeps that character as it is, for example\for a space or\|for a|that is not a pipe.
Operators in quotes are text: echo "a;b" prints a;b.
Variables¶
| You type | Result |
|---|---|
export NAME=value |
sets NAME for the shell and the commands it starts |
echo $NAME or echo ${NAME} |
the value of NAME |
echo $? |
the exit status of the last command |
echo $$ |
the process ID of the shell |
echo $! |
the process ID of the last background job |
unset NAME |
removes NAME |
env or printenv |
shows all variables |
Jobs¶
Each pipeline is a job.
| Key or command | Result |
|---|---|
| Ctrl+C | stops the job in the foreground (SIGINT) |
| Ctrl+\ | stops the job in the foreground (SIGQUIT) |
| Ctrl+Z | pauses the job in the foreground (SIGTSTP) |
jobs |
lists the jobs, with their number |
fg %1 |
continues job 1 in the foreground |
bg %1 |
continues job 1 in the background |
kill %1, kill 42 |
sends SIGTERM to job 1, or to process 42 |
kill -9 %1, kill -KILL %1 |
sends SIGKILL; a signal is a number or a name, with or without SIG |
When a background job ends or pauses, the shell tells you before the next prompt.
Built-in commands¶
cd, exit, history, export, unset, env, printenv, which,
jobs, fg, bg, kill, shift and set are in the shell. All other
commands are programs in /bin. which <command> tells which one a
command is.
cd with no argument goes to HOME. exit with no argument ends the
shell with the exit status of the last command.
Scripts¶
A script is a text file of commands. Run it with sh, or give it a
first line #!/bin/sh and make it executable:
$ cat /home/user/greet
#!/bin/sh
echo "hello $1, you gave $# arguments"
$ chmod +x /home/user/greet
$ /home/user/greet world
hello world, you gave 1 arguments
| Form | Result |
|---|---|
sh file a b |
runs file with the arguments a and b |
sh -c 'echo $1' name a |
runs one command line; $0 is name, $1 is a |
sh <file or sh -s a b |
reads the commands from the input |
sh -e file |
stops at the first command that fails |
In a script:
| You type | Result |
|---|---|
$0 |
the name of the script |
$1 to $9, ${10} |
the arguments |
$# |
the number of arguments |
"$@" |
each argument as its own word |
$* |
all arguments as one word, with spaces between them |
shift, shift 2 |
removes the first argument, or the first two |
set -e, set +e |
stops, or no longer stops, at a failed command |
exit 3 |
ends the script with exit status 3 |
With -e, a failed command before && or ||, or after !, does not
stop the script. A file without a #! line that is not a program also
runs as a shell script.
What the shell cannot do yet¶
if,for,while,caseand functions- file name patterns such as
*.c ~for the home folder$(command)and`command`NAME=valuewithoutexport- here-documents (
<<) - splitting the value of a variable into words