DEV Community

Cover image for a barely capable shell user

a barely capable shell user

Michael Tharrington on January 21, 2019

I started up my coding journey a little more than two weeks ago: baby steps... Michael Tharrington ・ ...
Collapse
 
kenbellows profile image
Ken Bellows • Edited

Awesome stuff, I love reading about people's early journeys into the world of the terminal 😎

You should definitely learn about your ~/.bashrc file if you haven't yet, it's a special file that gets run each time you open the terminal and it lets you set up a bunch of environment stuff to make your life easier, things like short aliases for common long commands and, importantly, environment variables.

And speaking of environment variables, the real reason I'm writing is that you can customize what your prompt (the Michaels-MacBook-Pro:~ dev$ bit) looks like using a variable called PS1. Google around and you'll quickly find more examples that you could want, but the biggest thing is that you can set it up to always print your entire path, or your path relative to your home directory, so you always know where you are without having to type pwd ever again!

A very simple, no-frills PS1 that will accomplish this is:

PS1='\u@\h:\w \$ '
Enter fullscreen mode Exit fullscreen mode

Try typing that into your terminal, and you should start to see this prompt (assuming your username is michael and your pwd is ~/code/dev):

michael@Michaels-MacBook-Pro:~/code/dev $
Enter fullscreen mode Exit fullscreen mode

If you like what it gives you, edit (or create) ~/.bashrc and paste it at the bottom. Then it will work in all future shell sessions, and you can say goodbye to pwd!

Here's a good overview of what the variables up there mean, and all the other ones you can use in your PS1: ss64.com/bash/syntax-prompt.html

Collapse
 
michaeltharrington profile image
Michael Tharrington

WOW! This is all amazing!! You just made my night, thanks so much. 😃

Collapse
 
kenbellows profile image
Ken Bellows • Edited

Btw, forgot to say this in the above comment: full paths can get pretty long, so what a lot of people (myself included) like to do is add a line break \n right before the $ to drop it onto a new line:

PS1='\u@\h:\w\n\$ '

michael@Michaels-MacBook-Pro:~/code/projects/some-repo-with-a-really-long-name
$ echo "Commands fit here no problem!"
Enter fullscreen mode Exit fullscreen mode

And if you want a little splash of color, there's a bunch of color codes in the link in my last comment, but to start off, I like to make my user@hostname part a different color from my path, maybe light blue for the former and yellow for the latter. And always remember to reset your color to the default with \e[0m at the end, unless you want all the text you type to be colored as well, if that's your thing.

PS1='\e[0;36m\u@\h \e[0;33m\w\n\e[0m\$ '
Enter fullscreen mode Exit fullscreen mode

your shell

Give that a try and see what you think!

Collapse
 
gavinfernandes2012 profile image
Gavin Fernandes

Thats great work with the terminal!
When I started out with C++ I was already a bit familiar with it since I used Ubuntu at the time, and we have command line package managers (instead of the app store). That being said, I still don't know some of the commands in your picture there 😂, but I do know quite a few languages (although I'm still learning).

Collapse
 
michaeltharrington profile image
Michael Tharrington

Thanks for the kind words, Gavin! And, I've actually been thinking about installing Ubuntu on my Lenovo - kinda wanna see what's outside the Mac/Windows box. I've yet to explore...

Collapse
 
gavinfernandes2012 profile image
Gavin Fernandes

Ohh I challenge you to a standard Arch Linux install. That's probably the quickest way to learn quite a bit about the command line and Linux at the same time. (We even have command line web browsers if you need to refer to the arch wiki, yeah that's a thing)

If you're really feeling daring, try Gentoo. Although your hair'll probably grow white before Chrome compiles 😂

Collapse
 
goyo profile image
Grzegorz Ziemonski

Congrats on getting over the terminal fear! Now your family and friends will think you're a super-hacker even if all you do is cd around and cat stuff.

Btw. I tried to cp -r test test and it gives me cp: cannot copy a directory, 'test', into itself, 'test/test' :(

Collapse
 
michaeltharrington profile image
Michael Tharrington

Hmmm... that is weird! I actually had to recreate this issue by memory because I kinda forgot how I did it the first time. Anyway, initially when I tried to recreate it I just went to type in cp test test and go the message:

cp: test is a directory (not copied).

But then I remembered to involve the -r and the infinite folder situation played out as expected. In fact, I just tested it again to make sure I'm not going crazy.

Could it be because I'm working with Terminal on Mac? Or possibly that I have no extra frills installed.

I'm on version 2.9.1:

my terminal version

Collapse
 
rpalo profile image
Ryan Palo

Nice! It's cool to see your journey and observations. If you want more examples, exercises, and introductions for the command line, Small, Sharp Software Tools by @bphogan.

is a really great resource to work through. Takes things slow, but explains things with real-world examples that make the lessons stick. :)

Collapse
 
michaeltharrington profile image
Michael Tharrington

Appreciate the kind words and thanks for sharing the resource! I'll definitely take a look!

Collapse
 
jacksonelfers profile image
Jackson Elfers

Nice work. I think most people learn commands on a need to know basis. If you can navigate the filesystem you're set to do whatever.

Collapse
 
michaeltharrington profile image
Michael Tharrington

Thanks! Good stuff! I feel pretty confident with these basic commands now and can get around pretty well. Glad to know the rest is really more of a need to know basis.

Collapse
 
jess profile image
Jess Lee

Awesome, Michael! I definitely had the same set of notecards a few years ago :)

Collapse
 
michaeltharrington profile image
Michael Tharrington

But were yours hand cut from loose leaf notebook paper? Haha, appreciate the encouragement! 😀