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)
lswhich ls/bin/lscd /bin./lsecho $PATHcd -
Shell variables
echo PATHecho $PATHPATH=~echo $PATHls(!)- put PATH back to original value
Manuals
man ls- man uses an external PAGER program, by default "less"
- less keys (similar to vi):
hhelpqquitSPACEscroll forward/searchnnext search hit
less ../stb-tester/stbt.pycat ../stb-tester/stbt.pyPAGER=cat man ls
- less keys (similar to vi):
- shell builtins:
man cd- also:
help cd
- also:
man manman passwdman 5 passwdman printfman 3 printf- "ls(1)" means the ls manpage in section 1
man hierfor 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./printenvSTBT_CONFIG_FILE=~/.config/stbt/stbt.conf./printenvSTBT_CONFIG_FILE=~/.config/stbt/stbt.conf ./printenv./printenvexport STBT_CONFIG_FILE=~/.config/stbt/stbt.conf./printenvenv - 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 oand type "hello" (RETURN) and "there" (RETURN) (on standard input)- Redirections
- http://www.gnu.org/software/bash/manual/html_node/Redirections.html
grep main < printenv.cRedirect inputgrep main < printenv.c > main.txtRedirect output
- Standard error
grep < printenv.c > main.txtYou wouldn't want the error going into main.txt- Not just for errors:
curl http://www.google.comcurl http://www.google.com > google.html
- Pipelines
- http://www.gnu.org/software/bash/manual/html_node/Pipelines.html
wc -land type a few lines, then Control-D on an empty linecat printenv.c | wc -lwc -l < printenv.ccat printenv.c | grep int | wc -l
job control
- http://www.gnu.org/software/bash/manual/html_node/Job-Control.html
sleep 600press Control-Csleep 600press Control-ZjobsStoppedbg %1jobsRunningman bashpage down a bit, then press Control-Zjobsfgthen press Control-Z againsleep 700 &jobskill %3Same as sending Control-C to a foreground processps fkill 1234Give the process id of the sleep process instead of "1234"
exit code
- Just an arbitrary number to communicate exit status to the calling process
./printargsecho $?- Modify printargs's main function to return 42
gcc printargs.c -o printargs./printargsecho $?- 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 okShort-circuiting boolean AND
man grepScroll 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/