Way of the Geek home
Your One Stop Geek Shop
  • Latest Caption

    Say What #10

    Latest Podcast

    No Podcast This Week
  • InnerGeek

  • A Vote For CHAOS!
    [September 24th 2008]

    A Vote For CHAOS!

  • Categories

    expand
  • Login





    Register Recover password
  • Member

  • Current Article

    Linux Users: Pimp Your Prompt (Part 1 of 2)
    Posted by Devin de Gruyl on May 23rd, 2007


    Ah, the lowly command-line interface. We’ve come so far from the days of MS-DOS, yet even on Windows Vista the CLI remains for many power users the quickest, most expedient way of performing all manner of maintenance-related tasks. Users of *nix systems especially have for years scoffed at GUI interfaces for system and network administration; even with today’s glossy GNOME and KDE environments, few things “feel” as warm and fuzzy to veteran “console commandos” as a good, solid text mode interface.

    There is, of course, one problem: Visually, most CLI environments look about as enticing as a bowl of plain oatmeal. No more! In this article and its inevitable sequel, I’ll show you a few quick and easy customizations to your CLI prompt, to make it look a helluva lot nicer.

    First, if you’re a relative newbie to the *nix world, you should know that the command line you’re using (or have seen others use) is more properly called a shell, an interpreter that translates what you type into commands your computer can understand, much like a parser in an old-school Infocom text adventure. This article focuses on the Bourne Again shell, or bash, which is the de facto standard shell program for virtually every GNU/Linux distribution out there; other shells include tcsh (popular in the BSD world), zsh, and csh.

    Let’s take a look at the standard bash prompt (the text that precedes your command inupt in a terminal window) on an Ubuntu-based Linux system:

    standardubuntuprompt1.png

    (The “username” and “localhost,” of course, will vary by system, but this is how it should look.)

    Not very pretty, but effective. It tells you who’s currently logged in, the hostame of the system, and the current directory you’re browsing in (the tilde, or ~ symbol, is *nix shorthand for the “home” directory, roughly analogous to the “My Documents” folder on a Windows XP computer). The $ before the cursor reminds you that you’re just a lowly user; if you had root access, this would be a #.

    Like CLI interfaces since the heady days of MS-DOS, it puts function ahead of form - it gets the message across with a minimum of effort. Which is fine, but sometimes you want to dress it up a little, y’know? Maybe add some more info, like the date or time of day, how many commands you’ve entered so far, or even a splash or two of color. The bash shell makes it possible for you to do this… and surprisingly, it isn’t as difficult as you might think, even though you do have to learn some fairly obscure codes to do it — but we’re here to make it a little bit less painful.

    The actual command to change your prompt to something else is fairly simple:

    firstexportcommand1.png

    This replaces your current prompt with this, which will undoubtedly cause old-school hackers to remember boarded-up white houses and mailboxes:

    yourfirstcustomprompt1.png

    You can enter any command you’d like, and the shell will always ask for your next command with the “>” prompt. Which isn’t bad, but neither is it very informative. For instance, you may want to display the current working directory, which is always useful information. So we add to it like this:

    secondexport1.png

    “\w” is a command code; it tells bash to insert the current working directory in the output. This will give you a prompt that looks like this:

    yoursecondprompt1.png

    Remember, the tilde means you’re in your home directory. Now, change to another directory and see what happens:

    seeingthatitworks1.png

    See how it works?

    To make the prompt show your current username, the command code to use is \u:

    usernameflag1.png

    Of course, you’re not limited to just one code per prompt:

    multipleflags1.png

    And you can add some text too, if you like:

    yourordersprompt1.png

    Having fun yet? Here’re some more command codes you can try:

    • \d - Shows the current date in the format “Wed May 23″
    • \h - Shows the top-level hostname (example: “myhost”)
    • \H - Shows the complete hostname (example: “myhost.mydomain.com”)
    • \n - Starts a new line (if you want your prompt to fit on multiple lines, you’ll need this)
    • \t - Shows the system time in HH:MM:SS format (24-hour clock)
    • \T - Also displays the system time, but in 12-hour (AM/PM) format
    • \xxx - Inserts standard ASCII character xxx
    • \# - Shows how many commands you have entered in the current session
    • \$ - If you are root, it displays a #; if not, it displays a $

    There are more codes than this available, but this should give you enough to play around with. Now we can get really fancy, like this:

    fancyprompt2.png

    Now, if you’ve put some real work and effort into a really spiffy-looking prompt, sooner or later you’re going to want to save it for your next login session as well. (The command we’ve been using only changes your prompt for the current session; if you logout and login again, you’ll be back at whatever you had for a prompt before you started your experimentation.) Doing this is a simple matter of editing the ~/.bashrc file (you should already have one, but if for some reason you don’t, just create one using your favorite text editor). Just add the following line anywhere in the file:

    PS1=”<your prompt code goes here>”

    (If the .bashrc contains any other lines like this one, just comment them out using # characters.)

    Logout, log back in, and you should see your new prompt staring you back in the face. Congratulations - you’ve taken your first steps toward making a *nix system truly your own!

    This is just the tip of the iceberg! In Part 2, coming hopefully soon, I’ll show you how to add such things as color to your prompt, and I’ll discuss some more useful command flags. In the meantime, just remember: Don’t fear the command line! 8^)

    Posted in code   | email this article 

    If you liked that, try...

    1. Linux Users: Pimp Your Prompt (Part 2 of 2)
    2. INX: Console Computing for Non-Console Users
    3. HOWTO: Code a Simple Game in Python
    4. Run Your Mac or Windows Fonts on Linux!
    5. Amazon MP3 Now Open To Linux Users

    You can leave a response

    No Comments »

    No comments yet.

    Leave a comment

    Captcha

    Enter the letters you see above.
    Can't see anything? Having problems? Email the admin

  • Contact Us

    Twitter Us!
    Podcast RSS
    EMAIL US!
    Podcast Voicemail:
    206-338-3288

    Our Podlinez Number:
    712-318-9815

    Find us on:

    Add our podcast to your iTunes
    Add our podcast to your Zune
    Find us on TPN
    Find us on Blubrry
  • Advertisement

    Advertise on Way of the Geek