Customizing Bash Shell Prompt
Transcript of Customizing Bash Shell Prompt
![Page 2: Customizing Bash Shell Prompt](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/2.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/3.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/4.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/5.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/6.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/7.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/8.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/9.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/10.jpg)
Now we can see that the prompt is just the empty string.
![Page 11: Customizing Bash Shell Prompt](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/11.jpg)
Now let’s change our Shell Prompt to the dollar sign followed by a space.
“$ ”
![Page 12: Customizing Bash Shell Prompt](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/12.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/13.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/14.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/15.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/16.jpg)
“\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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/17.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/18.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/19.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/20.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/21.jpg)
In Nano we reassign the variable PS1 to the value of the current user by using “\u”.
![Page 22: Customizing Bash Shell Prompt](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/22.jpg)
“\u” displays the user’s name. My user name is “jakorn” because my name is “Jake Corn”.
![Page 23: Customizing Bash Shell Prompt](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/23.jpg)
Capital “W” after the escape character “\” makes the shell prompt display the current working directory.
![Page 24: Customizing Bash Shell Prompt](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/24.jpg)
“\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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/25.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/26.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/27.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022062503/589ee56b1a28abe97f8b4757/html5/thumbnails/28.jpg)
Hope you find some good use of this slideshow.
Please Visit:
www.HatBoySoftware.Com