If I lived in the Harry Potter universe, my Patronus would be a turtle, because that's how much time I spend in the Shell. One of the many joys of using the command line for everything is modifying your Bash prompt - which is something I do often, just to keep things spicy! I'm excited to share a few of my favorite modifications with you.
If you're new to making changes in the command line, that's okay (and if you're not, you can skip this paragraph)! Just pop open your Terminal and make sure you're in your home directory. You can type cd and press enter, and if you were't there before, you are now. You can use your editor of choice, and if you don't have a favorite, go ahead and type nano .bash_profile and you'll be editing the file you need using the nano editor. You'll probably see a line that starts PS1=, so navigate down there, because that's the line we'll be editing.
If you're new to making changes in the command line, that's okay (and if you're not, you can skip this paragraph)! Just pop open your Terminal and make sure you're in your home directory. You can type cd and press enter, and if you were't there before, you are now. You can use your editor of choice, and if you don't have a favorite, go ahead and type nano .bash_profile and you'll be editing the file you need using the nano editor. You'll probably see a line that starts PS1=, so navigate down there, because that's the line we'll be editing.
- If you're like me, you do a lot of multitasking. I love having /W somewhere in my Bash prompt because it tells you the directory you're currently working in at a glance for when you get pulled away and need to be reminded where you are and what you're in the middle of. It's simple, but effective.
- You can include the output of a command in your bash prompt by putting the command in your prompt between `these guys`. A classic use case for this is to display a smiley face in your bash prompt if a command was successful and a frowny face if it fails. Here's what to put on your PS1= line to achieve the desired effect: \`if [ \$? = 0 ]; then echo '=)'; else echo '=('; fi\` You can change the faces to suit your preferences, if you're more of a :) or ^_^ person.
- Last one, and by far my favorite - color! Colorizing your Bash prompt looks a little crazy, so let me break it down. You'll need to start with \[ and end with \]. Next, you'll need to use \033[ or \e[ to signal to Bash that it should be looking for a color code to come next. The color code itself is a regular old integer, for example cyan is 36. Finally, you'll use the letter m to indicate the end of the color code. Putting it all together, use \[\033[36m\] to turn your Bash prompt cyan. All the text after that will be Cyan, unless you switch to another color, or "turn off" color by using 00 as the color code.
And that's it for some of my favorite Bash prompt modifications. Happy Bash scripting, gentlefriends!