CS246-F20-01-UnixShell
Lecture 1.4
• bash aliases
• Shells, subshells, your default shell
CS246
• Aliases give convenient names to commands you use a lot
– Not in original sh, but in bash, csh, tcsh, etc.
• I use personal aliases a lot (some are quite old!), for these purposes:
– Preload preferred options
– Shorten long commands that I use a lot
– Redirect an old name to the new application
alias 246=”ssh –Y –l cs246 linux.student.cs.uwaterloo.ca”
alias rm=”rm –iv”
alias p=less
Aliases (in bash)
Aliases
• Best to use quotes if command is more than one token long
$ alias d=date
$ d
Sun Apr 29 13:07:27 EDT 2019
$ alias now=”d”
$ now
Sun Apr 29 13:07:30 EDT 2019
$ alias off=”clear; exit”
$ off # clear screen before terminating shell
% alias # list all aliases; this is tcsh
246 (ssh -l cs246 linux.student.cs.uwaterloo.ca)
back cd –
c clear
cp (cp -iv)
du (du -h)
la (ls-F -AC)
lal (ls-F -Al)
ll (ls-F -l)
ln (ln -s)
ls ls-F
mv (mv -iv)
p less
pp (less +G -n)
rm (rm -iv)
ssh (ssh -Y)
Aliases
• Use aliases for command names or complete commands
– But use an environment variable to remember an arg/file name
$ alias lsMovies=”ls –l ~/Movies” # this works
$ lsMovies
total 9918128
-rw-r—r– 6 migod staff 246022054 8 Sep 09:36 Matrix.mp4
-rw-r—r– 6 migod staff 549392510 8 Jul 11:56 ToyStory.mp4
$ alias cs246assn=/u/jfdoe/cs246/a1 # but not this
$ cd cs246assn # alias only expands for command
bash: cd: cs246assn: No such file or directory
Aliases
• Aliases entered directly on command line disappear when the
current shell terminates L
• Two options for making aliases persist across sessions:
1. Insert the alias commands in the appropriate (hidden) .shellrc file in
your home directory e.g., .bashrc, .tcshrc
2. Place a list of alias commands in a file (often .aliases) and
source that file from the .shellrc file.
• Before changing your .bashrc or .tcshrc file, CREATE A BACKUP!
• Store any aliases you like a file called, say, $HOME/.aliases
• Put this line at the end of your .bashrc or .tcshrc file
– Then your aliases will be set up each time you start a new shell
– To get them into the current shell, execute source $HOME/.aliases at
the command line
cp $HOME/.bashrc $HOME/.bashrc-orig
alias rm=’rm –iv’ # this file is called .aliases
alias ls=’ls –FG’
source $HOME/.aliases
Side note: Using aliases
• exit terminates shell, with optional integer exit status (return code) N
– [ N ] is in range 0-255; larger values are truncated (256) 0, 257 ) 1,
etc.), negative values (if allowed) become unsigned (-1) 255)
– exit status defaults to zero if unspecified, which usually means success
– I get a lot of mileage out of just 0 and 1 as exit values
$ exit [ N ]
exit
• You can also start a sub-shell from a shell
– When the sub-shell exits, you are back in the original shell:
$ tcsh
% echo ${0} # what shell am I using ?
-tcsh
%
% exit
$ echo ${0}
bash
$
Shells, sub-shells, and
changing your default login shell
Shells, sub-shells, and
changing your default login shell
• When you log into UNIX at the command-line, you are started in an executing shell
in your home directory
– This is also true when you log in using one of the UNIX GUIs, like the GNOME
desktop; the GUI is spawned by a startup script from your login shell
– Your default login shell is already set for you, probably to bash
– You can change your default shell via chsh, tho you probably want to stay
with bash
$ chsh
Password: XXXXXX
Changing the login shell for jfdoe
Enter the new value, or press ENTER for the default
Login Shell [/bin/bash]: /bin/tcsh
End
CS246