2.18. Configuring the Look-and-Feel¶
In this section, we are going to finish setting up your scientific computing environment with a couple final items.
2.20. Terminal/Shell Startup Configuration: the .bashrc
file¶
Click the Terminal icon you added to the Panel in the last section. It’s the little black “TV set” on the right of the Panel (the “Task Bar”) which should be at the top of your screen.
We met the Terminal in the previous section. When you start it up, it opens a text-based window (i.e. not a button and menu GUI application; google “GUI” if you don’t know what it means).
The Terminal is ancient. It comes from time before we could talk with computers, before you could touch the screen to make your choice, before we carried computers in our pockets, even before you you could point at things on the screen with a mouse.
The Terminal comes from the Time of the Mainframe, when the computer was an enormous Machine housed in a large room which was modified to handle the huge power consumption and wiring. On every user’s desk was a Terminal, a little TV and a keyboard. The Terminal allowed users to sent text to the Machine, and receive text back, which was displayed on the screen of the little TV. We still have such Machines, now called supercomputers.
The Terminal is just the TV. The program that runs in the Terminal is called “The Shell”. There are many different unix shells; please read http://en.wikipedia.org/wiki/Unix_shell .
In this course we will be using the Bash shell. The shell is a program that reads the text you enter at the prompt, and interprests the commands, and carries them out. In the last section, we met the shell, the prompt, and a couple of commands (among them: ls, pwd, cd, and su).
The Prompt is the small line of text that looks like this:
[jhetrick@localhost ~]$
This is the shell telling you that it’s waiting for you to type a command and hit ENTER. In fact, we’ll change this rather generic prompt soon. For now, we have a few commands to issue in order to complete the customizations, before we study the shell in more detail.
When you first start the Terminal, which then starts the Shell, it looks for a special, hidden file, named .bashrc. This is a simple text file where you place configuration commands that will customize the behavior of the shell for you, as well as extend its functionality. For example, most people put aliases (shorthand names) for common commands.
I have prepared a simple .bashrc file for you with a few configuration lines. Once you feel proficient at unix, you can edit and change this file to your liking. Searching the web, you can find many (complex!) examples of .bashrc files that people use and have shared.
To use my .bashrc file, you must first get it.
We first have to get this file onto your VM. So, of course, we’ll just download it, but not through a browser.
I’m assuming you have have your VM up and running, and you are logged in. If not, do so.
Now, let’s see an example of how powerful the shell is.
Open a Terminal and at the prompt, copy/paste (or type) the following text (exactly):
curl -O dirac.physics.pacific.edu/phys/jhetrick/www/PHYS27/.bashrc
Note
If you set up Bidirectional Clipboard Sharing (above), you should be able to simply cut and paste the link above into the Firefox browser textbox.
Here we are using the program curl (Command line URL), which when you hit ENTER, goes out into the internet and grabs the URL you asked for. It is insanely powerful. The -O switch (more about switches later) lets you configure curl’s behavior at the command line. If you want you can tell curl to get a webpage , and then read that webpage for URLs, get those, and then read those...etc. Yeah, that’s pretty powerful.
If things worked, curl
should have responded with something like this:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1716 100 1716 0 0 83958 0 --:--:-- --:--:-- --:--:-- 86052
In particular you should see the Total Downloaded Size as 1716 bytes (second number).
Click the [X] button on the Terminal window to close it. Then, click the Terminal icon on the panel to open a new Terminal.
Now when the shell starts, it reads and executes the .bashrc
file,
and you should see a little message and a new prompt, like this:
Initializing from ~/.bashrc
>- path
>- prompt
>- aliases
User jhetrick - Welcome to the Machine
sci[~]>
Now we are ready to to do some science!
2.21. Examine .bashrc
¶
First, let’s look at the file you just grabbed. To do this, we’ll use less, a file pager/viewer. We’ll learn more about this and other commands in the next chapter, but for now, just type
less .bashrc
at the prompt in your shell window, then ENTER (from now on, if I say “type something at the prompt”, you will know that you follow that with ENTER, right?).
This will display the contents of the file .bashrc
, which should
look like the following.
To “page through” the file, go forward to the next page of text by hitting the SPACE bar. To go back and view the previous page, hit the b key.
#
# .bashrc initialization file
#
# This file created by Dr. J. Hetrick
# for PHYS 27/193 Introduction to
# Scientific Computing
#
# v. 4.0 Nov 2014
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
echo "Initializing from ~/.bashrc"
# --- setup PATH to executables
# if you want to add a directory to the
# PATH you add it at the end, with a :
export PATH=".:$PATH:$HOME/bin"
# -----------------------------
echo " >- path"
# --- setup BASH SHELL --------
# You can set the prompt to something useful by
# setting the variable PS1.
# \h means "Hostname" (the name of your computer
# \w means "working directory"
# This prompt has your hostname in it, useful
# when you are on different machines and want to
# know which one you are working on. However I've
# "commented it out" of this file by putting #
# in front of it.
#PS1="\h[\w]>"
# We can use this prompt for our Scientific VM
# It will have the working directory inside the []
# and I like a > for a prompt.
PS1="sci[\w]>"
# -----------------------------
echo " >- prompt"
# --- Put aliases here -------
alias l='ls -F'
alias la='ls -aF'
alias ll='ls -lF'
alias lla='ls -alF'
alias lsd='ls -lF | grep "^d"' # only directories
alias m=less
# this function lists the 20 most recently changed files
function lt() { ls -lFt "$@" | head -20 ;}
# ----------------------------
echo " >- aliases"
# ----------------------------
# comment this out if too cheezy for you :)
# by putting a # at the front. Notice it
# echos the value of the USER environment variable
echo "User $USER - Welcome to the Machine"
First, every line that begins with a hash (#) is a comment. It will not be read as a command, but rather is there solely for the reader.
After the top comment block describing the file, there is an if statement:
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
This command effectively says “if there is a system bashrc (``/etc/bashrc``) file, read and execute its commands first”.
Next comes some comments about the PATH, and this command:
export PATH=".:$PATH:$HOME/bin"
This command adds the current folder (.
) and a bin
subfolder to
the PATH–a colon (:) separated list of folders in which the shell
should look for executable programs.
Next, we change the prompt to something friendlier. The default prompt is: [user@localhost ~]$
.
I prefer the following:
sci[~]>
What this does is let you know that you are currently logged into your
Scientific environment (which we’re going to call sci). Since I
often login to other machines around the world via the terminal (and
start a shell on that machine), I always put the same .bashrc
file
on the remote machine, except that I change the name in the prompt to
the name or location of that machine, like dirac[~]>
or
hawaii[~]>
.
Additionally, this prompt shows that you are currently working in your Home directory (or folder), which has a nickname: ~ (the tilde).*Folders* (where files are stored) in Unix are called Directories. I will use the two terms interchangably, and you will get used to it. Just remember:
Note
Directory = Folder
If you cd to another directory, such as your Pictures directory (do this now, by typing cd Pictures
), you should see that the prompt changes to
sci[Pictures]>
Did it? To move back to your Home directory, all you have to do is type cd
(+ ENTER), and your
prompt should return to
sci[~]>
In this little exercise, in addition to learning how to move around a little, you’ve learned that the tilde–in the context of directories–is a shorthand name for Your Home (or Default) Directory.
Note
~ = your Home (default) directory
After setting the prompt (by defining the variable PS1=”sci[w]>”), the .bashrc
file contains alias
definitions.
alias l='ls -F'
alias la='ls -aF'
alias ll='ls -lF;
...
These are short hand names for common commands. As you already learned
the ls
command lists the contents of a directory. You can change
it’s behavior by adding switches, various letters prepended by a
minus sign (like -F, -alqF, -O, etc.).
For example, at your prompt, type ls
.
Now type a single l
(ell). It should show roughly the same thing,
except that in the latter case, you typed the alias for ls
-F
. The -F
switch tells ls
to “format” the output, by adding
a trailing slash to directories, so that it is easier to distinguish
them from simple files.
Now type ll
(ell ell). This version gives much more detail:
permissions, owner, group, size, date, etc. This is called a “long
listing” and hence the alias ll
.
Similarly, la
will show ALL files, including hidden ones that
start with a dot. Try it: type
la
2.23. Fonts and Colors¶
Finally, I like to play with the font and color a little. This customization is completely up to you.
To change the font that the Terminal uses, go to the very top of the window and click “Settings”, then click “Edit Current Profile”.
You can change almost everything about the Terminal, and I recommend staying away from everything but font and color.
Click the “Appearance” tab and play around with the options. You can always click “Cancel” to go back to what it was before. If you move the “Edit Profile” window to the side, you can see the effect of your choices on the Terminal. Personally, I like a bold, 11 point, Monospace font, in a stark White-on-Black color scheme. But my eyes are old and need the print to pop.
You are now ready to start using your scientific computing environment.