How to change the Bash Command Prompt using PS1 variable
One of the first things I do after having a fresh Installation, is to change the command prompt to something I like. I know most of you linuxers would want to make the prompt look cooler than the usual "name-hostname: / $". But command prompt is not all about coolness, it serves a lot more useful purposes than it appears. In this howto, I will show you how to change your command prompt by editing PS1 environment variable and make it useful and cool at the same time
.
In order to change the prompt you should know that the PS1 environment variable is responsible for it. Setting its value will change the command prompt. In order to know what the current value of this environment variable is, run the following command
[shredder12]$ echo $PS1
This will show you something like
[\u@\h \W]\$
and if its a debian or ubuntu system then the output will look like
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}shredder12:\w\$
I know it looks scary but once understand the basics you can easily play around with this.
Editing the PS1 environment variable
Let us start with the first example.
[\u@\h \W]\$
Here '\u' means the username of the current user, \h stands for the hostname upto the first '.' and \W stands for the base name of the current working directlory with your $HOME(the user home directory) directory being specified as ~(tilda). '@' and '$' are ascii characters. We will learn more terminology soon, but for now lets stick to these three.
So, now if the username is "shredder12", hostname is "my.computer" and the current directory is /home/shredder12/Documents then the bash prompt will look something like.
[shredder12@my ~/Documents]$
As you can see its really simple, using similar attributes we can make more changes in our prompt. Say, you want to add time and date too in the prompt, we will use /t and /d attributes to do that. Now, the PS1 environment variable will look like
[\u@\t \d]$
and your prompt will be like this
[shredder12@11:47:19 Mon Jan 18]$
Saving the new PS1 variable in a bash script file
Now, you can easily change the PS1 variable to suit your needs but you will have to make the changes in the bash scripts to make them permanent.
If you are on a multiuser system then I would suggest you to add this line at the end of your $HOME/.bashrc file
export PS1="[\u@\t \W]$"
and then either run
source $HOME/.bashrc
or exit the terminal and open it again.
If you want to make these changes global, then you should edit your /etc/bash.bashrc or /etc/bashrc file, whichever exists, depending upon the distro you are using. But remember that the global changes in these files can be overwritten by the the $HOME/.bashrc script, to suit the specific user needs.
List of useful backslashed special character attributes to be used with PS1
Following is a list of the attributes that you can use in the PS1 variable.
\t - time
\d - date
\n - newline
\s - Shell name
\W - The current working directory
\w - The full path of the current working directory.
\u - The user name
\h - Hostname
\# - The command number of this command.
\! - The history number of the current command
If you are interested in more bash tricks, take a look at this article.





, but in case you have to, then ImageMagick will make this job really easy for you. ImageMagick is a great opensource command line utility for image manipulation.





















Post new comment