BSD DevCenter
oreilly.comSafari Books Online.Conferences.


Customizing the Login Shell
Pages: 1, 2

Another difference between the two shells is their use of the history command. Log in as test2 to enter the C shell, then type


to view your command line history. It will look something like this:

     1	su
     2	exit
     3	whoami
     4	su root
     5	grep root /etc/passwd
     6	whoami
     7  su test1
     8	exit
     9	logout
    10	whoami
    11	exit
    12	h

Now try this, to repeat command number 10:


And this, to repeat the last command:


If you want to repeat the last command that starts with "log":


You can save yourself a lot of typing with the history command.

Now, log in as test1, and try the same history commands. You'll be disappointed to discover that h doesn't work in the Bourne shell. You need the C shell to get the added history functionality of ! Fortunately, root gets the C shell by default.

Test1 isn't out of luck, though. As test1, type csh -- your prompt will change to:


Now, let's try history again:

   1  h

This is disappointing, though. By default, a user's history doesn't follow him when he changes shells. Let's make a bit of history and then try some commands:


   1  h
   2  whoami
   3  h

If test1 gets tired of the C shell, she can type exit and receive her Bourne shell back.

Now let's do something useful with all of this knowledge. Let's make sure test1 receives a fortune when he logs into his login Bourne shell. While we're at it, we'll let test2 also receive a fortune when she exits her login C shell. First, we need to know where fortune lives, so we can add a command to execute it:

whereis fortune
fortune: /usr/games/fortune

The Bourne shell supports login scripts, not logout scripts. The name of the Bourne shell's login script is .profile. As test1:

pico /usr/home/test1/.profile

Add the following line to the end of this file:


and save your changes. Then type exit to log out and log back in as test1.

If you get a fortune, you've just written your first login script on a Unix system.

The C shell supports both login and logout scripts. Not surprisingly, the files are named .login and .logout. Log in as test2 and try:

ls -a

Note that .login was created with the user; however, you must create your own .logout if you want commands to execute when you log out of your login C shell. To view the file, type:

more .login

And you should see that it ends with a command to execute /usr/games/fortune. Let's create a logout script:

pico .logout

Add the line:


and save your change. Now logout and you should have a parting fortune to read before receiving your new login prompt.

One last exercise to tie together all these concepts. As root,

pico /root/.logout

At the bottom of the file, add the line:


and save your changes. Log in and log out as root to see if you can get your fortunes both coming and going.

Now, log in as test2 and su to root. Did you receive a fortune when you entered the non-login shell? How will you leave this non-login shell? Will you receive a fortune when you leave the non-login shell?

If you enjoyed this article, I highly recommend that you check out the O'Reilly book, Unix Power Tools. This is the type of book you can grow into. There are plenty of helpful tips for those new to Unix, and the stuff you don't understand now you can go back to and use later as you progress from newbie to pretty good user to Unix guru.

Next week we'll continue familiarizing ourselves with FreeBSD by introducing some essential commands.

Dru Lavigne is a network and systems administrator, IT instructor, author and international speaker. She has over a decade of experience administering and teaching Netware, Microsoft, Cisco, Checkpoint, SCO, Solaris, Linux, and BSD systems. A prolific author, she pens the popular FreeBSD Basics column for O'Reilly and is author of BSD Hacks and The Best of FreeBSD Basics.

Read more FreeBSD Basics columns.

Discuss this article in the Operating Systems Forum.

Return to the BSD DevCenter.


Sponsored by: