Administration
  Home arrow Administration arrow Page 2 - Customizing the User Environment in BS...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ADMINISTRATION

Customizing the User Environment in BSD
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2006-12-07

    Table of Contents:
  • Customizing the User Environment in BSD
  • Hack 2: Useful tcsh Shell Con
  • Hack 3: Create Shell Bindings
  • Hack 4: Use Terminal and X Bindings

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Virtual Tradeshows by Ziff Davis Enterprise - A Unique Opportunity to Connect with IT Experts, Access Information, and Gain Insight on today's Technology

    Customizing the User Environment in BSD - Hack 2: Useful tcsh Shell Con
    (Page 2 of 4 )

    Make the shell a friendly place to work in.

    Now that you’ve had a chance to make friends with the shell, let’s use its configuration file to create an environment you’ll enjoy working in. Your prompt is an excellent place to start.

    Making Your Prompt More Useful

    The defaulttcshprompt displays%when you’re logged in as a regular user andhostname#when you’re logged in as the superuser. That’s a fairly useful way to figure out who you’re logged in as, but we can do much better than that.

    Each user on the system, including the superuser, has a .cshrc file in his home directory. Here are my current prompt settings:

      dru@~:grep prompt ~/.cshrc
     
    if ($?prompt) then
              set prompt = "
    %B%n@%~%b: "

    That isn’t the defaulttcshprompt, as I’ve been using my favorite customized prompt for the past few years. The possible prompt formatting sequences are easy to understand if you have a list of possibilities in front of you. That list is buried deeply withinman cshrc, so here’s a quick way to zero in on it:

      dru@~:man cshrc
      /prompt may include

    Here I’ve used the / to invoke the manpage search utility. The search string prompt may include brings you to the right section, and is intuitive enough that even my rusty old brain can remember it.

    If you compare the formatting sequences shown in the manpage to my prompt string, it reads as follows:

      set prompt = "%B%n@%~%b: "

    That’s a little dense. Table 1-1 dissects the options.

    Table 1-1. Prompt characters

    Character

    Explanation

    "

    Starts the prompt string.

    %B

    Turns on bold.

    %n

    Shows the login name in the prompt.

    @

    I use this as a separator to make my prompt more visually appealing.

    Table 1-1. Prompt characters (continued)

    Character

    Explanation

    %~

    Shows the current working directory. It results in a shorter prompt than %/, as my home directory is shortened from /usr/home/myusername to ~

    %b

    Turns off bold.

    :

    Again, this is an extra character I use to separate my prompt from the cursor.

    "

    Ends the prompt string.

    With this prompt, I always know who I am and where I am. If I also needed to know what machine I was logged into (useful for remote administration), I could also include %M or %m somewhere within the prompt string.

    The superuser's .cshrc file (in /root, the superuser's home directory has an identical prompt string. This is very fortunate, as it reveals something you might not know about the su command, which is used to switch users. Right now I’m logged in as the user dru and my prompt looks like this:

      dru@/usr/ports/net/ethereal:

    Watch the shell output carefully after I use su to switch to the root user:

      dru@/usr/ports/net/ethereal: su
     
    Password:
      dru@/usr/ports/net/ethereal:

    Things seem even more confusing if I use the whoami command:

      dru@/usr/ports/net/ethereal: whoami
     
    dru

    However, the id command doesn’t lie:

      dru@/usr/ports/net/ethereal: id
     
    uid=0(root) gid=0(wheel) groups=0(wheel), 5(operator)

    It turns out that the default invocation of su doesn't actually log you in as the superuser. It simply gives you superuser privileges while retaining your original login shell.

    If you really want to log in as the superuser, include the login (-l) switch:

      dru@/usr/ports/net/ethereal: su -l 
      Password:
      root@~: whoami
     
    root
      root@~: id
     
    uid=0(root) gid=0(wheel) groups=0(wheel), 5(operator)

    I highly recommend you take some time to experiment with the various formatting sequences and hack a prompt that best meets your needs. You can add other features, including customized time and date strings and command history numbers [Hack #1], as well as flashing or underlining the prompt.

    Setting Shell Variables

    Your prompt is an example of a shell variable. There are dozens of other shell variables you can set in .cshrc. My trick for finding the shell variables section in the manpage is:

      dru@~:man cshrc
      /variables described

    As the name implies, shell variables affect only the commands that are built into the shell itself. Don’t confuse these with environment variables, which affect your entire working environment and every command you invoke.

    If you take a look at your ~/.cshrc file, environment variables are the ones written in uppercase and are preceded with the setenv command. Shell variables are written in lowercase and are preceded with the set command.

    You can also enable a shell variable by using thesetcommand at your command prompt. (Useunsetto disable it.) Since the variable affects only your current login session and its children, you can experiment with setting and unsetting variables to your heart’s content. If you get into trouble, log out of that session and log in again.

    If you find a variable you want to keep permanently, add it to your ~/.cshrc file in the section that contains the defaultsetcommands. Let’s take a look at some of the most useful ones.

    If you enjoyed Ctrl-d from “Get the Most Out of the Default Shell” [Hack #1], you’ll like this even better:

      set autolist

    Now whenever you use the Tab key and the shell isn’t sure what you want, it won’t beep at you. Instead, the shell will show you the applicable possibilities. You don’t even have to press Ctrl-d first!

    The next variable might save you from possible future peril:

      set rmstar

    I’ll test this variable by quickly making a test directory and some files:

      dru@~:mkdir test
      dru@~:cd test
      dru@~/test:touch a b c d e
             
     

    Then, I’ll try to remove the files from that test directory:

      dru@~/test:rm *
     Do you really want to delete all files? [n/y]

    Since my prompt tells me what directory I’m in, this trick gives me one last chance to double-check that I really am deleting the files I want to delete.

    If you’re prone to typos, consider this one:

      set correct=all

    This is how the shell will respond to typos at the command line:

      dru@~:cd /urs/ports 
      CORRECT>cd /usr/ports (y|n|e|a)?

    Pressingywill correct the spelling and execute the command. Pressingnwill execute the misspelled command, resulting in an error message. If I presse, I can edit my command (although, in this case, it would be much quicker for the shell to go with its correct spelling). And if I completely panic at the thought of all of these choices, I can always pressato abort and just get my prompt back.

    If you like to save keystrokes, try:

      set implicitcd

    You’ll never have to typecdagain. Instead, simply type the name of the directory and the shell will assume you want to go there.

    More Administration Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "BSD Hacks," published by O'Reilly. We hope...
     

    Buy this book now. This article is excerpted from the book BSD Hacks, written by Dru Lavigne (O'Reilly; ISBN: 0596006799). Check it out today at your favorite bookstore. Buy this book now.

       

    ADMINISTRATION ARTICLES

    - Configuring Load-Balanced Clusters
    - Load-Balanced Clusters
    - UNIX Time Format Demystified
    - Making Changes in the CVS
    - Building Your First CVS Repository
    - CVS Quickstart Guide
    - Authorizing Users in Samba
    - Handling User Accounts in Samba
    - Authentication in Samba
    - Accounts, Authentication, and Authorization
    - Advanced Concepts on Dealing with Files and ...
    - Dealing with Files and Filesystems
    - More Hacks for the User Environment in BSD
    - Personalizing the User Environment in BSD
    - Customizing the User Environment in BSD




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway