====== Shell Programming ====== sed $ sed “s/day/night/” new $ sed “s/day/night/; 14 q” new # chain commands, quit after line 14 $ sed -E "s/\{s:([0-9]*)[[:space:],]*l:([0-9]*)[[:space:] ,]*n:([0-9]*).*/\1\t\2\t\3/g" /dev/null & $ kill %2 # kill job number 2 $ fg %2 # put job 2 in the foreground $ bg %2 # put job 2 in the background $ export PS1='$ ' # set prompt $ . activate # dot command, run in parent context from within script $ source activate # same as dot command ==== Directory Stack ==== $ pushd $PWD # push current directory onto the stack $ cd /home/whatever $ popd # return to pushed directory ==== Redirection ==== $ cat >newfile.txt # redirect stdout, replace file $ cat >>newfile.txt # redirect stdout, append to file $ cat &newfile.txt # redirect stderr, replace file $ cat &>newfile.txt # redirect stdout and stderr, replace file $ cat &>>newfile.txt # redirect stdout and stderr, append to file $ cat newfile.txt # copy a file ==== Pipe ==== $ ps -e | grep ping # redirect stdout of first command to stdin of second command $ ps -e | grep ping | less # chain pipes ==== chmod ==== $ chmod +x ~/.local/bin/myscript.sh # make a script executable ==== chown ==== $ chown root myfile.txt # change owner of myfile to root ==== How to stop a program ==== Signaling Kill ==== How to start a program ==== $ $ bash $ source $ . $ exec $ & $ nohup $ systemd $ nice ==== foreground, background, daemon ==== run in background run as background job redirect stdout and stderr $ disown %1 # suppress SIGHUP and remove from jobs list $ disown -h %1 # suppress SIGHUP $ nohup # suppress SIGHUP when starting command ==== glossary ==== * session * state Ss * terminal session * one foreground process * multiple background processes * multiple background jobs