How to Make a Custom Command Prompt: A Quick Command Line Tutorial
Posted on Sat, 08/29/2015 - 14:42
I recently finished Codecademy's course "Learn the Command Line" that I mentioned in another recent post. After going through all the lessons and exercises, I really have to say again that this course is an awesome introduction to using the command line to be more productive, especially if you are an often timid command line newbie like me.
After finishing the course, I thought it would be cool to share something that I learned here on my blog. One thing I learned was how to get creative with customizing your own command line environment.
In this post, I'll show you how to customize your command prompt, the little character(s) that let's you know the command line interface is ready to receive your next command. Here's what a normal command prompt might look like on your computer, when you open up the Terminal program (Mac OS X) or Command Prompt program (Windows):
The "command prompt" that we're referring to here is the little "$" symbol. This is the character string that we'll be learning how to customize on our own computers.
The style/appearance of the command prompt is controlled by one of many variables that can be stored/customized in a file called "~/.bash_profile". Here the "~" refers to your computer user home directory. And the ".bash_profile" is the hidden file that we'll be editing.
To start editing this file, type the following command in your command line interface (in the Terminal program on a Mac or the Command Prompt program in Windows) and then press "Enter" on your keyboard:
This opens up the "~/.bash_profile" file inside a very basic text editor program called Nano on your command line interface. Here, you'll be able to edit the "~/.bash_profile" file.
Now, on a blank line in the file, type the following:
export PS1="¯\_(ツ)_/¯ "
Feel free to copy and paste the exact text above on your own computer, or create your own custom command prompt by typing in your own character string inside the quotation marks. Here, "PS1" just refers to the name of the variable that defines how our command prompt looks.
After you've typed out the line defining your PS1 variable, you need to save your "~/.bash_profile" file. To save a file inside the Nano text editor, press "Ctrl + O" on your keyboard (Note: that's the letter 'O'), and then press "Enter" to write the filename.
Once your file is saved, press "Ctrl + X" on your keyboard to exit the Nano text editor program. Now, back in the normal command line interface, type the word "clear" and hit "Enter" in order to clear everything off your command line interface.
Finally, type the following command and press "Enter":
This command activates the changes you made in your "~/.bash_profile" file, allowing you to see your new command prompt immediately.
And that's it! You should now be seeing your brand new, custom command prompt showing up in your command line interface!
If you entered the same value for the PS1 variable that I did above, you should see something like this: