Linux distribution

How to Customize (and Colorize) Your Bash Prompt

Most Linux distributions configure the Bash prompt to look something like username@hostname:directory$ . But you can configure the Bash prompt to contain whatever you like, and even choose whatever colors you like.

The example steps here were performed on Ubuntu 16.04 LTS. The process should be the same on other Linux distributions, although the default Bash prompt and settings in the .bashrc file may be a bit different.

Where the Prompt Variable is Stored

Your Bash prompt configuration is stored in your user account’s .bashrc file, which is at ~/.bashrc. So, if your username is bob, the file is at /home/bob/.bashrc.

You can open the file to view the current Bash variable. We’ll use nano as our example text editor, although you could also use vi, emacs, or any other text editor you’re comfortable with. Open a Terminal and run:

nano ~/.bashrc

Scroll down to the PS1= section. The first variable looks rather complicated because it includes color information—we’ll explain that later. The second variable, without color information, reads as follows:

${debian_chroot:+($debian_chroot)}\u@\h:\w\$

This is still a little complicated due to the ${debian_chroot:+($debian_chroot)} bits. These just tell Bash to let you know if you’re using a Debian chroot environment and normally won’t be shown. Ignoring those, here’s the default structure of the Bash prompt variable:

\u@\h:\w\$

\u indicates your username, @ indicates the @ sign, \h indicates the hostname (computer name), : indicates the : character, \w indicates the working directory, and \$ indicates a $ if you’re a normal user account or # if you’re root. So, putting that all together, you get username@hostname:working_directory$.

To change your Bash prompt, you just have to add, remove, or rearrange the special characters in the PS1 variable. But there are many more variables you can use than the default ones.

Leave the text editor for now—in nano, press Ctrl+X to exit. We’ll show you how to experiment with variables before actually writing a new one into your .bashrc file.

How to Create a Custom Bash Prompt

Your Bash prompt configuration is stored in the PS1 variable. To save the contents of the PS1 variable into a new variable, run the following command:

DEFAULT=$PS1

You can now set the PS1 variable to different values to experiment. For example, the first line here would set your prompt to a basic “user$” prompt, while the second would set your prompt to a basic “user:working_directory$” prompt.

PS1="\u\$ " PS1="\u:\w\$ "

If you ever want to get back to your default prompt, just run the following command.

PS1=$DEFAULT

Bash will be restored to its default prompt thanks to the fact that you saved those default settings earlier. Note that any changes you make here are only temporary for the current Bash session, so you can always sign out and sign back in or close and reopen the terminal window to go back to your default prompt. But the above line makes it possible to easily get back to your default Bash prompt without the hassle of signing out or closing a window.

You can add any characters or text to the variable. So, to prefix the default prompt with “Hello World”, you could use:

PS1="Hello World \u@\h:\w\$ "

Now that you’ve got the basics down, you just need to know what all the special characters are. You probably won’t care about many of these, but here’s the full list as it appears in the Bash manual:

  • A bell character: \a
  • The date, in “Weekday Month Date” format (e.g., “Tue May 26”): \d
  • The format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required: \D{format}
  • An escape character: \e
  • The hostname, up to the first ‘.’: \h
  • The hostname: \H
  • The number of jobs currently managed by the shell: \j
  • The basename of the shell’s terminal device name: \l
  • A newline: \n
  • A carriage return: \r
  • The name of the shell, the basename of $0 (the portion following the final slash): \s
  • The time, in 24-hour HH:MM:SS format: \t
  • The time, in 12-hour HH:MM:SS format: \T
  • The time, in 12-hour am/pm format: \@
  • The time, in 24-hour HH:MM format: \A
  • The username of the current user: \u
  • The version of Bash (e.g., 2.00): \v
  • The release of Bash, version + patchlevel (e.g., 2.00.0): \V
  • The current working directory, with $HOME abbreviated with a tilde (uses the $PROMPT_DIRTRIM variable): \w
  • The basename of $PWD, with $HOME abbreviated with a tilde: \W
  • The history number of this command: \!
  • The command number of this command: \#
  • If the effective uid is 0, #, otherwise $: \$
  • The character whose ASCII code is the octal value nnn: \nnn

How to Repair GRUB2 When Ubuntu Won’t Boot

Ubuntu and many other Linux distributions use the GRUB2 boot loader. If GRUB2 breaks—for example, if you install Windows after installing Ubuntu, or overwrite your MBR—you won’t be able to boot into Ubuntu.

You can easily restore GRUB2 from a Ubuntu live CD or USB drive. This process is different from restoring the legacy GRUB boot loader on older Linux distributions.

This process should work on all versions of Ubuntu. It’s been tested on Ubuntu 16.04 and Ubuntu 14.04.

The Graphical Method: Boot Repair

Boot Repair is a graphical tool that can repair GRUB2 with a single click. This is the ideal solution to boot problems for most users.

If you have the media you installed Ubuntu from, insert it into your computer, restart, and boot from the removable drive. If you don’t, download a Ubuntu live CD and burn it to a disc or create a bootable USB flash drive.

When Ubuntu boots, click “Try Ubuntu” to get a usable desktop environment.

Ensure you have an Internet connection before continuing. You may need to choose a Wi-Fi network and enter its passphrase.

Open a Terminal window from the Dash and run the following commands to install and launch Boot Repair:

sudo apt-add-repository ppa:yannubuntu/boot-repair sudo apt-get update sudo apt-get install -y boot-repair boot-repair

The Boot Repair window will automatically scan your system after you run the boot-repair command. After it scans your system, click the “Recommended repair” button to repair GRUB2 with a single click.

You can choose to use the advanced options here, but Ubuntu’s wiki recommends you not use the advanced options unless you know what you’re doing. The recommended repair option can fix most…

The Best Keyboard Shortcuts for Bash (aka the Linux and macOS Terminal)

Bash is the default command-line shell on most Linux distributions, from Ubuntu and Debian to Red Hat and Fedora. Bash is also the default shell included with macOS, and you can install a Linux-based bash environment on Windows 10.

The bash shell features a wide variety of keyboard shortcuts you can use. These will work in bash on any operating system. Some of them may not work if you’re accessing bash remotely through an SSH or telnet session, depending on how you have your keys mapped.

Want to Learn More? Get Linux Training from the Linux Foundation

Learn everything you could want to know about Linux with comprehensive online Linux courses and certification tests that include the real-world experience and first-hand knowledge of what it takes to be a successful Linux developer or system administration professional, all from a web browser in the comfort of your home.

The Linux Foundation is the nonprofit consortium dedicated to fostering the growth of Linux. Founded in 2000, they are supported by the leading technology companies and developers from around the world.

Working With Processes

Use the following shortcuts to manage running processes.

  • Ctrl+C: Interrupt (kill) the current foreground process running in in the terminal. This sends the SIGINT signal to the process, which is technically just a request—most processes will honor it, but some may ignore it.
  • Ctrl+Z: Suspend the current foreground process running in bash. This sends the SIGTSTP signal to the process. To return the process to the foreground later, use the fg process_name command.
  • Ctrl+D: Close the bash shell. This sends an EOF (End-of-file) marker to bash, and bash exits when it receives this marker. This is similar to running the exit command.

Controlling the Screen

The following shortcuts allow you to control what appears on the screen.

  • Ctrl+L: Clear the screen. This is similar to running the “clear” command.
  • Ctrl+S: Stop all output to the screen. This is particularly useful when running commands with a lot of long, verbose output, but you don’t want to stop the command itself with Ctrl+C.
  • Ctrl+Q: Resume output to the screen after stopping it with Ctrl+S.

Moving the Cursor

Use the following shortcuts to quickly move the cursor around the current line while typing a command.

  • Ctrl+A or Home: Go to the beginning of the line.
  • Ctrl+E or End: Go to the end of the line.
  • Alt+B: Go left (back) one word.
  • Ctrl+B: Go left (back) one character.
  • Alt+F: Go right (forward) one word.
  • Ctrl+F: Go right (forward) one character.
  • Ctrl+XX: Move between the beginning of the line and the current…

How to Maximize Your Linux Laptop’s Battery Life

Laptop manufacturers spend a lot of time tuning their device drivers for Windows battery life. Linux usually doesn’t get the same attention. Linux may perform just as well as Windows on the same hardware, but it won’t necessarily have as much battery life.

Linux’s battery usage has improved dramatically over the years. The Linux kernel has gotten better, and Linux distributions automatically adjust many settings when you’re using a laptop. But you can still do some things to improve your battery life.

Basic Battery-Saving Tips

Before you do anything too complex, adjust the same settings you would on a Windows laptop or MacBook to maximize battery life.

For example, tell your Linux laptop to suspend—this is what Linux calls sleep mode—more quickly when you’re not using it. You’ll find this option in your Linux desktop’s settings. For example, head to System Settings > Power on an Ubuntu desktop.

Screen brightness can affect battery life dramatically. The brighter your display backlight, the worse your battery life will be. If your laptop has hotkeys to change screen brightness, try them—they’ll hopefully work on Linux, too. If not, you’ll find this option somewhere in your Linux desktop’s settings. It’s available at System Settings > Brightness & Lock on Ubuntu.

You can also tell your Linux desktop to turn off the screen more quickly when it’s inactive. The laptop will use less power when its screen is off. Don’t use a screensaver, as those just waste power by making your computer do more work and leaving the display on.

You can also disable hardware radios you don’t use. For example, if you don’t use Bluetooth, you can disable it to gain some more battery life. Head to System Settings > Bluetooth to disable Bluetooth on an Ubuntu desktop.

If you’re not using Wi-Fi, you can save a bit of power by disabling that, too. On Ubuntu, head to System Settings > Network and enable “Airplane Mode” to disable Wi-Fi and other wireless radios.

Remember that what you do with the laptop is also important. Running heavier software and using more CPU resources will cause your laptop to use more battery power. For this reason, you may want to look at a more lightweight desktop environment, such as the Lxde-based Lubuntu instead of the Unity-based main Ubuntu desktop.

Install Proprietary Graphics Drivers (If You Need Them)

If your laptop has integrated Intel graphics, congratulations. You shouldn’t need to worry about power management issues with your graphics drivers. Intel graphics aren’t the fastest, but they have excellent open-source driver support and “just work” out of the box.

If your laptop has NVIDIA or AMD graphics, however, you may need to do some work to decrease power consumption.

The worst case scenario is a laptop with NVIDIA Optimus or AMD’s switchable graphics. Such laptops have two different GPUs. For example, an NVIDIA Optimus laptop will have both a more powerful, battery-draining NVIDIA GPU and a less powerful, battery-friendly Intel GPU. On…