28
BASH$: Customizing Shell Prompt [email protected]

Customizing Bash Shell Prompt

Embed Size (px)

Citation preview

Page 1: Customizing Bash Shell Prompt

BASH$:Customizing Shell Prompt

[email protected]

Page 2: Customizing Bash Shell Prompt

Environment Variables

Dynamically update the shell prompt with the

Current Date, User, Time, Command Number or a

string value of your choice.

“PS1” is the name of the variable that the Mac Terminal uses to Store the String value that will be the prompt

By Default PS1 is the host(computer name) followed by the current directory followed by the user’s name.

Page 3: Customizing Bash Shell Prompt

Environment Variables

Dynamically update the shell prompt with the

Current Date, User, Time, Command Number

or a string value of your choice.

Export PS1 = “”Clears the Prompt

Export PS1 = “\d”Displays Current Date

Export PS1 = “\t” Displays Current Time

Export PS1 = “\h”Displays Host Machine Name

Page 4: Customizing Bash Shell Prompt

Environment Variables

Dynamically update the shell prompt with the

Current Date, User, Time, Command Number

or a string value of your choice.

Export PS1 = “\#”Displays Current Command Number In

Session

Export PS1 = “\u”Displays User Name

Export PS1 = “\W” Displays Current Working Directory

Export PS1 = “\w”Displays Current File Path

Page 5: Customizing Bash Shell Prompt

I’ve used “pwd” to print the working directory

Default Bash Shell Prompt:The Default Bash Shell Prompt Shows up as the HostName (computer’s name) followed by the Working Directory followed by the User’s Name.

Page 6: Customizing Bash Shell Prompt

I’ve used “pwd” to print the working directory and then used the command “nano” to either open (if it exists) or create (if it doesn’t) a file named “.bash_profile”

Editing Bash Profile with the Nano Editor.

bash_profile is a configuration file for the bash shell. This file can be used to export variables in shell. The variable we are working with is PS1 which represents our prompt in Shell.

We can use the Nano editor to alter bash defaults.

Page 7: Customizing Bash Shell Prompt

This is the “Nano” editor. By typing:

nano .bash_profile

We are using the Nano program to either open or create a file called “.bash_profile”

Page 8: Customizing Bash Shell Prompt

We export the variable PS1 as an empty string to Shell to get rid of all information in the shell prompt.

Page 9: Customizing Bash Shell Prompt

To save and get out of Nano editor simply hold “Control”(^) and press “o” to write(save) the file. After that is done, we exit by holding “Control(^)” and pressing “x” to exit the Nano Program.

Exiting “Nano”:

Page 10: Customizing Bash Shell Prompt

Now we can see that the prompt is just the empty string.

Page 11: Customizing Bash Shell Prompt

Now let’s change our Shell Prompt to the dollar sign followed by a space.

“$ ”

Page 12: Customizing Bash Shell Prompt

You could make the shell prompt any string value you’d like. I like to use a concise shell prompt because it removes clutter from the screen.

Page 13: Customizing Bash Shell Prompt

We can import variables into a string by using the escape character “ \ ”.

To make our Shell Prompt the current time followed by a space, reassign the variable PS1 to the value “\t ” which is the current time plus a space.

Page 14: Customizing Bash Shell Prompt

It looks like this which is a display of military time at the start of each prompt. This could be helpful if you’re in constant need of time information.

Page 15: Customizing Bash Shell Prompt

The current date can be displayed by changing the value stored in the PS1 variable of our .bash_profile file by writing…

export PS1 = “\d ”

“d” means date.

Page 16: Customizing Bash Shell Prompt

“\h ” once again uses the “escape character ( \ ) ” to import variable “h”. “\h” represents the hostName of the computer. My computer is named jcMac.

Page 17: Customizing Bash Shell Prompt

And we see the prompt display the “host name” for my computer which is “jcMac” at the beginning of each new line.

Page 18: Customizing Bash Shell Prompt

We can also COUNT the number of commands in our current session by using the “\#” variable in our “.bash_profile”

Make sure you are in root directory, if not, type “cd” and hit enter. Then Type:

nano .bash_profile

And update the PS1 variable as shown below. Save(^o) and exit(^x)

Page 19: Customizing Bash Shell Prompt

And we are here at command number 1.

I’ll enter some commands to see our counter in action.

Page 20: Customizing Bash Shell Prompt

I’ve used ls to list the contents of the current directory. And afterwards we can clearly see that our prompt has incremented to “2” as this is our second command.

Page 21: Customizing Bash Shell Prompt

In Nano we reassign the variable PS1 to the value of the current user by using “\u”.

Page 22: Customizing Bash Shell Prompt

“\u” displays the user’s name. My user name is “jakorn” because my name is “Jake Corn”.

Page 23: Customizing Bash Shell Prompt

Capital “W” after the escape character “\” makes the shell prompt display the current working directory.

Page 24: Customizing Bash Shell Prompt

“\W” in action… you can see the current working directory displayed at the beginning of each command.

I changed directories around a bit to show the variable in action.

Page 25: Customizing Bash Shell Prompt

You can also update the PS1 variable to the value of “\w” to display the path name in the prompt. Very helpful to always have a context of current location, though this could fill up the screen quickly.

Page 26: Customizing Bash Shell Prompt

And this is the prompt I find myself using at the moment of writing this. It is a counter followed the working directory and a colon “ : ” .

Page 27: Customizing Bash Shell Prompt

See! It looks good, and the counter is helpful because my eyes can scan the left side of the screen for numbers if I need to look back.

Page 28: Customizing Bash Shell Prompt

Hope you find some good use of this slideshow.

[email protected]

Please Visit:

www.HatBoySoftware.Com