CS246-F20-01-UnixShell
Lecture 1.3
• Commands in bash
– type/which
– echo
– “Globbing” aka command-line pattern expansions
CS246
• Generally, if you execute a shell interactively, you are running
command-line programs with options and arguments (args
are often filenames)
• A command can be one of (in order or searching):
a) an alias (i.e., a simpler name) for built-in/external, possibly with pre-
supplied arguments, e.g., alias rm=’rm -iv’
b) built-in to the shell, e.g., echo, type
c) an external program, i.e., an executable file sitting on the file system,
e.g., g++, cp, rm, ls, mkdir, emacs, vim, nethack
Commands in the UNIX shell
Blue text means
a user typed this
Shell prompt: $ means bash, % means tcsh
$ cmd –opt1 –op2 arg1 –opt3 arg2 arg3 arg4
• Given this command:
– First, the shell first looks for an alias named cmd
– If none, then it looks for a shell built-in command named cmd
– If none, then it looks for external programs along the user’s PATH
named cmd
• There is a predefined shell variable called PATH that the shell
uses to search for external programs, in order. For example:
/usr/bin, /bin, /usr/local/bin, $HOME/bin etc.
– The value of PATH is typically set within the .bashrc file, which is
executed when the shell starts up
Commands in the UNIX shell
$ cmd –opt1 –op2 arg1 arg2 arg3
type / which
• Want to know what the command flurble means in your
current context?
– Is it an alias for “flurble –frobozz”?
– Is it built-in to the shell?
– Is it an executable file? If so, where?
• For tcsh / bash, “which flurble”
– For bash only, “type flurble” might give you a bit more info
$ pwd # Still inside an executing bash shell
/Users/migod/temp
$ ls
Hearts balloon balloon.cc oldStuff
$ which ls
/bin/ls
$ ls -F
Hearts@ balloon* balloon.cc oldStuff/
$ ls -F -G
Hearts@ balloon* balloon.cc oldStuff/
$ echo $PATH
/usr/local/bin:/Users/migod/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/loc
al/bin:/opt/X11/bin:/usr/X11/bin:/usr/texbin
$ tcsh # let’s start a subshell of tcsh
% which ls # I have an alias already set up
ls: aliased to ls-F
% which ls-F
ls-F: shell built-in command.
% ls
Hearts@ balloon* balloon.cc oldStuff/
$ echo no place like HOME
no place like HOME
$ echo ” no place like HOME” # quotes, space
no place like HOME
$ echo “no place like $HOME”
no place like /Users/migod
$ echo ‘no place like $HOME’
no place like $HOME
$ echo no place like $HOME
no place like /Users/migod
echo
• echo write arguments, separated by spaces and terminated
with newline
– It’s usually a very good idea to use double or single quotes to enclose
the message
• Generally, if you run a shell, you are running command-line programs:
• You can access the command line arguments programmatically using the
argv and argc parameters to the main function in a C/C++ program
• Globbing patterns apply only to args (often, file names), not commands or
options
• Note that the examples here assume bash; other shells may have slightly
different syntax or return different results if matching fails
Globbing:
Command-line pattern expansions
$ command –opt1 arg1 –op2 arg2 arg3
• Special chars for matching multiple files or constraining choices
• Suppose the current dir is /u/jfdoe/cs246/a1 which contains the files
q1x.C , q2y.h , q2y.cc , q3z.cpp
* matches 0 or more characters
? matches 1 character
Globbing
$ echo q* # “q*” matches filenames, echo prints ’em
q1x.C q2y.h q2y.cc q3z.cpp
$ echo q*.??
q2y.cc
{. . .} matches any alternative in the set
$ echo *.{C,cc,cpp}
q1x.C q2y.cc q3z.cpp
[. . .] matches 1 character in the set
$ echo q[12]*
q1x.C q2y.h q2y.cc
[!. . .] matches 1 character not in the set
$ echo q[!1]*
q2y.h q2y.cc q3z.cpp
Globbing Globbing
Can create ranges using hyphen
[0-3] => 0 1 2 3
[a-zA-Z] => lower or upper case letter
[!a-zA-Z] => any character that is not a letter
Hyphen is escaped by putting it at start or end of set
[-?*]* # => matches file names starting with -, ?, or *
* doesn’t match dotfiles (for your own good!)
ls *fnord* will find xxfnordster but not .fnordster, if
they exist in the current dir
$ ls *.pdf *.txt
# will list all PDF and plain text files in the current dir
$ rm *.docx *.doc *.pptx *.ppt
# will remove all MS-Word and Powerpoint files (if any) in cur dir
$ cd /Users/migod/Music/iTunes/iTunesMusic/BiebersGold
$ ls *[Bb]ieber*.mp3
I-love-Justin-Beiber-fanSong.mp3
Bieber-SingsBruceSpringsteen-concert-full-3hours.mp3
lalalabieberlalala.mp3
• Globbing is turned off inside single and double quotes
– Single quotes: Everything up to next single quote is protected (incl. NL,
double quotes)
– Double quotes: Protect everything except doublequote, backquote,
and $VARs
% cd temp/
% ls
Hearts> balloon* balloon.cc oldStuff/
% echo My balloon* home is $HOME
My balloon balloon.cc home is /Users/migod
% echo My “balloon* home is $HOME”
My balloon* home is /Users/migod
% echo My ‘balloon* home is $HOME’
My balloon* home is $HOME
Globbing and quoting
End
CS246