Chapter 8: The Shells
Boiled down:
PS1 |
PATH |
history |
set |
variables revisted quotes * |
.bash_profile |
unset |
` |
Recall
- < filename
- > filename
- >> filename
- &> filename - redirects standard output and error to filename
- cat x y 1> hold1 2 > hold2
- redirect stdout and stderr to different files
- cat x y &> hold
- redirect stdout and stderr to single file
- cat x y 1> hold1 2 >&1
- redirects standard output to hold and declare file descriptor 2 to be a
duplicate of file descriptor 1
Interactive shells
- When you login, an interactive (bash) shell shows you a command prompt
-
Command history
- ^z, fg, bg, command &
- When you run a shell script, you get a non-interactive shell, without history of job control
- bash myScript.sh
- ./myScript.sh
Changing shells
-
Recommended to stay with bash - but try the others by invoking them at the command line
- Use chsh (change shell) to change your login shell
- bash - Bourne-again shell
- csh - actually runs tcsh (a superset)
- sh - actually runs bash (a superset)
- ksh - not available
- dash - some features of ksh
Environment Variables
- env or printenv - display all of the environment variables and their values
-
set - displays all variables for this shell, including local variables
-
Child processes do not get copies of local variables
-
Unless you export them
- workDir=/home/SRUNET/deborah.whitfield/cs207
- export workDir
- workDir=/tmp
Customizations
- Customizations are usually added to a file called .bash_login or .bash_profile
in your home directory (obsidian is .bash_profile)
-
PATH=$PATH:.
-
PS1="\h: "
-
PS1='[$PWD]'
-
SHELL="/bin/csh"
aliases
-
Customized commands - less typing
-
See Table 8.2
-
alias - displays a list of currently aliased commands
-
alias f="find . -name \"*.txt\" .print.
-
alias spell="/usr/bin/hunspell"
Command History
- history - shows all commands recorded
- history 5 - shows the last five commands
- !! - reexecute the last command
- !find - reexecutes the last find command
- Arrow (cursor control) up and down
- Edit using left & right arrows, then insert/delete
- Lots more in Table 8.3
~ and set
- ~ is a shorthand for your home directory
- cd ~
- cd ~deborah.whitfield
-
set -o noclobber
- Prevents overwriting a file using > or >>
Writing a shell script
- edit startup file (.bash_profile, .profile, .login, .bashrc etc.)
to set PATH
- PATH=$PATH:.
- PS1="\h:\w: "
- set -o noclobber
- alias h="cd $HOME"
- who | grep whitfield
- Source the startup file to run changes
Scripting
- put Unix commands into file to create a script
- Start with #!/bin/sh
- chmod u+x file to make script executable
- ./script to run file
Parameters and Variables
- myvar=abc
- sets myvar to abc, but you cannot use spaces
- to access user defined variables - $myvar
- Keyword Variables - $HOME, $PATH
- Use " " if you want spaces in string
- myvar="abc 123"
- echo $myvar
- abc 123
- But if multiple spaces...
- myvar="abc and 123"
- echo $myvar
- abc and 123
- echo "$myvar"
- abc and 123
- * is used as wildcard. If a directory listing included two files
that start with m (m1 and m2), then ls m* would display m1 and m2
- myvar=m*
- echo "$myvar"
- m*
- Without quotes ...
- echo $myvar
- m1 m2
- unset myvar
- removes a variable
- readonly myvar
- cannot give myvar a value
- Variable attributes can be assigned with the declare statement
- declare -r myvar="I am a constant"
- -a declares as an array
- -f declares as function name
- -r makes variable read only
Command Substitution
- `whomai`
- when placed in a script will execute the whoami command
- myvar=`whoami`
- sets the variable to the output of the whoami comand
- myvar=`whoami`.`pwd`
Separator and newline
- ; newline separator
- \ continues a command
Regular Expression - Wild card characters
- * - match 0 or more
- ? - match 1 character
- LOTS more to come!
All work herein is subject to copyright. Original content to Dr. Deborah Whitfield, text content (Your UNIX/Linux) to Prentice Hall publishing.