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.
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
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
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.
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.
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”
We export the variable PS1 as an empty string to Shell to get rid of all information in the 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”:
Now we can see that the prompt is just the empty string.
Now let’s change our Shell Prompt to the dollar sign followed by a space.
“$ ”
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.
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.
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.
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.
“\h ” once again uses the “escape character ( \ ) ” to import variable “h”. “\h” represents the hostName of the computer. My computer is named jcMac.
And we see the prompt display the “host name” for my computer which is “jcMac” at the beginning of each new line.
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)
And we are here at command number 1.
I’ll enter some commands to see our counter in action.
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.
In Nano we reassign the variable PS1 to the value of the current user by using “\u”.
“\u” displays the user’s name. My user name is “jakorn” because my name is “Jake Corn”.
Capital “W” after the escape character “\” makes the shell prompt display the current working directory.
“\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.
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.
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 “ : ” .
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.
Hope you find some good use of this slideshow.
Please Visit:
www.HatBoySoftware.Com