Get terminal / bash prompt to show useful info

Problem: Well it’s more of an irritation. My new hosting provider does allow SSH access, but when logged in the bash prompt looks like this, regardless of which account  I’m logged-in as, or where I am:

-bash-4.2$

What I want is for it to show something like:

username@server:/current/path$

Solution: The bash prompt can be customised via a profile script, ideally anything that loads when you log in like ‘.bash_profile’, ‘.bashrc’ or ‘.profile’. If one of these files doesn’t already exist in your home directory (check with ls -la ~ to list the contents) then create one. Here is an example of creating, editing and loading a login script to show a better prompt.

Step 1: Create the file and open it for editing.

$ touch ~/.bash_profile
$ nano ~/.bash_profile

Notes:
– Deciding which file to create probably depends on your OS flavour. Maybe Google it? I searched for “centos 6 user login script” and found that when using an “interactive login shell” these two files are read: ‘.bash_profile’ and ‘.profile’.
– `touch` creates the file with owner and permissions given to the currently logged-in user.
– `nano` is a nice easy to use text editor. If it’s not available, try `vim`, or if you hate yourself use `vi` and spend 30 mins googling the correct commands to save and exit the file again.

Step 2: Add the following to the file, then save it.

export PS1="\u@\h:\w$ "

Notes:
– An explanation of the special characters that can be used with the prompt can be found under ‘PROMPTING’ on the bash man page. But for brevity:
‘u’ = username of current user.
‘h’ = the hostname up to the first ‘.’.
‘w’ = the current working directory, with $HOME abbreviated with a tilde.

Step 3: Make the change take effect.
Just logging-out and logging-in again should show your changes. Maybe a more solution is to load the script again with the following command:

$ source ~/.bash_profile

Notes:
– If this has no effect, it might be that ‘.bashrc’ or ‘.profile’ should have been used for your server.

Result: On my server, when logged in right now I get the following:

ben@myhost:~/httpdocs/wp$

which is much better than:

-bash-4.2$

Any questions?

Please let me know if this post was useful with a click!
Nope, not helpful...Yep, more useful than not! - +1 thumb, 1 overall.
Loading...

Leave a Reply

Your e-mail address will not be published. Required fields are marked *