Published on (
 See this if you're having trouble printing code examples

FreeBSD Basics

Customizing the Login Shell


For those users whose first operating system was not Unix, the concept of different login shells can be very confusing. It can take some time before you can consistently remember how to figure out which shell you are in, know which shells should be used for which purposes, and become comfortable with the features of each shell.

This article won't cover shell scripting or advanced shell tweaks; instead we'll concentrate on some differences between the C and Bourne shells. We'll also cover editing login and logout scripts, and learn something about login and non-login shells along the way.

By default, on a FreeBSD system, root's login shell is csh (the C shell) and all other users are given a default login shell of sh (the Bourne shell). If you haven't customized your prompt, its appearance is a clue to which shell you are using. To test this, let's make two users. Log in as root and type:


and select "Configure," "User Management," and then "User."

Note that the only section filled in is the Login shell field; it shows the default of a new user as /bin/sh.

Create a user called test1, add the user to the wheel group, assign a password you'll remember, and tab over to "OK" to add the user.

Create a second user called test2, add the user to the wheel group, assign a password, but this time change the Login shell field to read /bin/csh. Then tab over to "OK" to add the second user.

Exit /stand/sysinstall and type the word logout. Your prompt should now look like this:


Login as test1 and you'll receive the Bourne shell which looks like this for a normal user:


Now, su to root to see what the shell looks like to the superuser:


Note that hostname will be replaced with the hostname of your computer.

Open up another virtual terminal that has the login: prompt. This time, log in as test2 to see what the C shell prompt looks like to a regular user:


Now, su to root to see what the shell looks like to the superuser:


Note that both superuser's prompts look the same. So how do you tell which user has which login shell? Try this:

grep test* /etc/passwd
test1:test1:*:1005:0:User &:/home/test1:/bin/sh
test2:test2:*:1006:0:User &:/home/test2:/bin/csh

You may have noticed when you logged in as test2 that you received a humorous fortune; however, test1 and both superusers did not. Before we discuss why, if you type logout as either superuser, you should get this error message:

Not a login shell.

and you'll still have the same prompt. To leave superuser, you must type exit. Then test1 should be back here:


(the Bourne shell) and test2, who has the C shell, should get this prompt back:


Let's try this one more time; have test1 su to root, then:

grep root /etc/passwd
root:*:0:0:Charlie &:/root:/bin/csh

If test2 does the same thing, you should get the same results. The reason why both the superuser's prompts looked the same is that they both received root's default C shell.

Now for the login shell error message. Open up another virtual terminal with a login: prompt and log in directly as root.

Since root's login shell is the C shell, you should receive a fortune and a prompt that looks like this:


Now type logout. You should receive the login: prompt back without any error messages.

This is an important concept. You can only logout of a terminal that you login to. If you su to another user, you will receive that user's default shell, but it will be as a non-login shell. The only way to exit a non-login shell is with the exit command.

Open up yet another virtual terminal with a login: prompt and log in as test1. Your prompt should look like this:



su test2

and enter the password for test2 when prompted. Your prompt should change to this:


Note that your shell has changed from the login Bourne shell to the non-login C shell. If you type:


you will see

Not a login shell.

However, if you type exit, your prompt changes back to


Now type logout -- you should see:

logout: not found

What happened now? You are in the Bourne login shell but it seems to have "lost" the logout command! We better try a:

man logout

If you go down a page, you'll see a list of commands and a comparison of those built into the C and the Bourne shells. Note that the C shell supports many more commands than the Bourne shell. Finally, note the Bourne shell's lack of built-in support for the logout command. The only way to leave the Bourne shell is with the exit command.

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.


Copyright © 2009 O'Reilly Media, Inc.