Lecture Three
Pathnames; Filename Expansion; Shell Basics
Pathnames
- absolute pathnames start with
/
, representing the root, and continue with the directory structure to the destination filename or directory name - relative pathnames do not start with
/
, and are relative to your current (or working) directory - relative-to-home pathnames are actually absolute, but use
~
to represent the home directory~
replaced by$HOME
, eg.~/pathname
~user
replaced by home directory of user, eg.~user/pathname
Filename Expansion
- also called ambiguous file references, metacharacters, wild card characters, and filename generation characters
- used to find filenames that match a pattern
?
matches any single character, eg.echo temp?2
*
matches any number (including none) of characters, eg.ls temp*
- a leading period (hidden file) must be explicitly specified, eg.
echo *
- will not show hidden files [ ]
matches any single character in included list, eg.ls temp[12]*
-
within[ ]
between two characters represents a range, eg.ls temp[1-58]
is the same asls temp[123458]
- if
!
is first character within[ ]
, then any character not in list is matched
Shell Basics
- command history
or
- move to previous command or next commandfc -l
- display last 16 commandshistory
- display all commands in buffer!num
- re-execute command number "num"!xxx
- re-execute last command beginning with string "xxx"
- some useful BASH keyboard shortcuts:
- erase Characters: Backspace or Ctrl-Backspace or Ctrl-h
- go to the beginning of the line: Ctrl-a
- go to the end of the line: Ctrl-e
- delete a word before the cursor: Ctrl-w
- delete everything before the cursor: Ctrl-u
- clear screen: Ctrl-l
- search for a keyword in previous commands: Ctrl+r
- auto-complete file/directory names: Tab
Quoting
- one reason for quotes is so that string values can contain spaces and special chars
- three styles of quoting:
- single quotes ' ' (strong quotes - don't allow variable and command substitution)
- double quotes " " (weak quotes - allow variable and command substitution)
- backslash \ (quotes the next character only)
quoting examples:
==> school=Seneca # Assign a variable ==> mySchool=my school is $school # This will cause an error bash: school: command not found ==> mySchool="My school is $school" # Weak quotes, allowing substitution ==> echo $mySchool My school is Seneca ==> mySchool='My school is $school' # Strong quotes, not allowing substitution ==> echo $mySchool My school is $school ==> mySchool=My\ school\ is\ $school # Equivalent of weak quotes ==> echo $mySchool My school is Seneca ==> mySchool=My\ school\ is\ \$school # Equivalent of strong quotes ==> echo $mySchool My school is $school ==> dirListing=$(ls -l) # Command substitution ==> echo $dirListing # Spacing is not preserved total 0 -rw------- 1 lczegel users 0 Oct 10 17:54 file1 -rw------- 1 lczegel use rs 0 Oct 10 17:54 file2 -rw------- 1 lczegel users 0 Oct 10 17:54 file3 ==> echo "$dirListing" # Spacing is preserved total 0 -rw------- 1 lczegel users 0 Oct 10 17:54 file1 -rw------- 1 lczegel users 0 Oct 10 17:54 file2 -rw------- 1 lczegel users 0 Oct 10 17:54 file3 ==> _