Unix shell basics
By David Röthlisberger. Comments welcome at david@rothlis.net.
Last updated 4 Oct 2012. These notes are placed in the public domain.
Rough notes for a half-hour course on Unix shell basics.
Run a program (ls)
ls
which ls
/bin/ls
cd /bin
./ls
echo $PATH
cd -
Shell variables
echo PATH
echo $PATH
PATH=~
echo $PATH
ls
(!)- put PATH back to original value
Manuals
man ls
- man uses an external PAGER program, by default "less"
- less keys (similar to vi):
h
helpq
quitSPACE
scroll forward/
searchn
next search hit
less ../stb-tester/stbt.py
cat ../stb-tester/stbt.py
PAGER=cat man ls
- less keys (similar to vi):
- shell builtins:
man cd
- also:
help cd
- also:
man man
man passwd
man 5 passwd
man printf
man 3 printf
- "ls(1)" means the ls manpage in section 1
man hier
for a somewhat old-fashioned description of the file-system hierarchy
arguments
cat printargs.c
#include <stdio.h> int main(int argc, char** argv) { int i; for (i = 0; i < argc; ++i) { printf("--- arg %d: ---\n", i); printf("%s\n", argv[i]); } return 0; }
gcc printargs.c -o printargs
./printargs hello world
expansion
- Shell Parameter Expansion
- Single Quotes
- http://www.gnu.org/software/bash/manual/html_node/Single-Quotes.html
./printargs 'hello world'
./printargs '$PATH'
- Double Quotes
- http://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
./printargs "$PATH"
./printargs "$PATH hello world"
./printargs $PATH hello world
- Does word splitting happen before or after parameter expansion?
DAVE="a b c"
./printargs $DAVE
./printargs "$DAVE"
- Filename Expansion
- http://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html
./printargs *.c
./printargs *
ls *
- Other Shell Expansions
environment variables
cat printenv.c
#include <unistd.h> #include <stdio.h> extern char** environ; int main(int argc, char** argv) { char** env; for (env = environ; *env; ++env) { printf("%s\n", *env); } return 0; }
gcc printenv.c -o printenv
./printenv
STBT_CONFIG_FILE=~/.config/stbt/stbt.conf
./printenv
STBT_CONFIG_FILE=~/.config/stbt/stbt.conf ./printenv
./printenv
export STBT_CONFIG_FILE=~/.config/stbt/stbt.conf
./printenv
env - A=1 B=2 ./printenv
standard input, output & error
- Standard Input is where input data comes from, often the keyboard.
- Standard Output is where output data goes, often the terminal screen.
grep o
and type "hello" (RETURN) and "there" (RETURN) (on standard input)- Redirections
- http://www.gnu.org/software/bash/manual/html_node/Redirections.html
grep main < printenv.c
Redirect inputgrep main < printenv.c > main.txt
Redirect output
- Standard error
grep < printenv.c > main.txt
You wouldn't want the error going into main.txt- Not just for errors:
curl http://www.google.com
curl http://www.google.com > google.html
- Pipelines
- http://www.gnu.org/software/bash/manual/html_node/Pipelines.html
wc -l
and type a few lines, then Control-D on an empty linecat printenv.c | wc -l
wc -l < printenv.c
cat printenv.c | grep int | wc -l
job control
- http://www.gnu.org/software/bash/manual/html_node/Job-Control.html
sleep 600
press Control-Csleep 600
press Control-Zjobs
Stoppedbg %1
jobs
Runningman bash
page down a bit, then press Control-Zjobs
fg
then press Control-Z againsleep 700 &
jobs
kill %3
Same as sending Control-C to a foreground processps f
kill 1234
Give the process id of the sleep process instead of "1234"
exit code
- Just an arbitrary number to communicate exit status to the calling process
./printargs
echo $?
- Modify printargs's main function to return 42
gcc printargs.c -o printargs
./printargs
echo $?
- The shell considers a 0 exit code as success, anything else as failure.
- Also used by shell looping & conditional constructs:
if ./printargs; then echo ok; else echo failed; fi
./printargs && echo ok
Short-circuiting boolean AND
man grep
Scroll down to EXIT STATUS near the end
Further reading
- I recommend reading the original manuals instead of unreliable googled tutorials.
- intro(1) i.e. type:
man 1 intro
- bash(1) -- but use it as a reference; don't attempt to read it cover-to-cover
- http://www.gnu.org/software/bash/manual/
- Do read sections "Shell Commands", "Shell Expansions", "Redirections", and "Builtin Commands".
- GNU coreutils categorised index:
http://www.gnu.org/software/coreutils/manual/coreutils.html
- and in particular the last chapter, "Opening the Software Toolbox": http://www.gnu.org/software/coreutils/manual/coreutils.html#Opening-the-software-toolbox
- Not all Unix (or even Linux) systems use the GNU user-space tools
- Busybox is common in the embedded-Linux world: http://busybox.net/downloads/BusyBox.html
- Fedora System Administrator's Guide (if you're using Fedora): http://docs.fedoraproject.org/en-US/Fedora/17/html/System_Administrators_Guide/