Linux Tip | How to Customize Your BASH Environment

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
greetings and salutations and thank you for clicking on this video today we're going to talk about customizing your bash environment this video is going to be for intermediate to more advanced Linux users those of you who are familiar with the concept of editing text files somebody who has worked in the terminal for a while may find this very useful because what I'm going to show you today is going to add a lot of functionality to your experience as you use the bash shell in a terminal if you are brand-new to Linux I encourage you to go ahead and watch this video anyway because you're going to learn about some of the possibilities and some of the things that you can do down the line to customize things should you choose to start working in a terminal somebody posted on one of my videos not too long ago that I shouldn't talk about the terminal I shouldn't show it because it scares off new Linux users because they're afraid of the terminal so before we get into the video let me say yes you can load a linux mint or Manjaro or a core or any one of those distributions that are designed to be very new user friendly and chances are you won't have to open up the terminal much but the bash terminal is a big part of the linux world it is a big part of being part of the linux ecosystem and for a lot of people it is the main attraction because it is so powerful and it allows you to do things with your computer with just a few commands that would actually be very difficult to do in a GUI you don't have to use the terminal you don't have to learn it but on the flip side you don't have to be a programmer either to get something out of it so I wanted to say that up front and another thing I must say is that I am suffering from a cold and I will try and keep the sniffing and snorting and coughing and hacking down to a bare minimum but should I do so somewhere along the way in this video please forgive me in advance but I wanted to go ahead and do the video while all the information was nice and fresh in my mind and get it out there a lot of people have expressed interest in this video so I wanted to go ahead and do so do forgive me if I sound a little funny here okay so let's jump into talking about what the environment is when you open up a terminal whether it be a terminal emulator like we have here or whether you log into a TTY however you get into your account there is a whole big bunch of variables that get loaded in the background and as you work in bash you can access those variables to do different things it's simple things like a past statement for instance where the system is going to look for programs it's all kinds of stuff in here and if you want to look at your environment you can print the environment and the command is print env and in this case I'm going to pipe it into a pager to make it easier to deal with there's a whole bunch of stuff in here it tells us a lot of cryptic information about how we have things set up so here for instance you see the path statement here's your path statement this is where the system is going to look for commands when you type them in and you can add to the path statement and you can also change things in here like the umask which is how files are assigned permissions when you create a file so there's a whole lot here to deal with we're going to be focusing on just two main things that you can do because I think they are the most useful and we'll touch on other things as we roll along this is by no means the definitive guide ladies and gentlemen this is definitely going to be an overview the idea here is to introduce you to concepts and then you can go off and do your research because this is all extremely well-documented this is also non distribution specific so whatever distribution of Linux you're using you can take advantage of a lot of this stuff - is bash it doesn't matter what operating system it runs on it could be Linux UNIX Mac Windows makes no difference it is bash so the first thing that I want to talk about here is aliases and they are really nifty little things that you can do to create commands or you can redefine them and and let me show you a really simple alias so with aliases you can like I said redefine a command so what would we do with an alias lo well we issue the alias command okay and this is the syntax so we're going to give it the name of our command that we're going to do in this case we're going to do DF and the reason why we're going to do that is because this is a really good example of modifying a command so if I issue DF which is disk free you'll notice that it gives it back to me in one kilobyte chunks so that's what those numbers mean it's not very easily readable to a human being I mean if you can do real quick math in your head you can break that down to megabytes and gigabytes so what we're going to do is we are going to and this H flag to that and what that's going to do is it's going to give us humanly readable output well if every time that you issue the DF command you don't want to remember to actually put that H on there then what you can do is to just simply alias the command and include that so we'll do that real quick here so it's an its alias DF and then it's equals and then in single quotations this is very important DF H which is the command that we want it to be and so we hit enter we don't get any output so it obviously worked now when I type in DF then it will always do it in humanly readable form there are some advantages to using aliases this way and there are some disadvantages first of all if I have an alias for a command that already exists what if I need to run DF and I don't want the humanly readable output I have some crazy need to actually see it in blocks like that in one kilobyte blocks then well I'm kind of screwed so that's something to keep in mind it's actually much better to create a completely new file name if you're going to modify a command so most Linux systems a boon to systems already have this alias in here everybody knows about LS okay and when i type LS then it lists the storage in my home directory but what if I want LS to show me a more detailed view well you probably know about LS and then l okay and that will give me a long listing of all the files well if you're running in a butsu system or maybe even many other systems as well you might be able to just type ll that's an alias for the LS command with the long function there and it will give you a lot more output plus the ll command shows you the dot files as well so it's everything in a directory now ll doesn't exist it is an alias that is defined by the system and so therefore you can't do a commit you can't do something like this and look for which ll there's nothing there it doesn't exist so that's another downside to aliases we can also use aliases to create new commands by putting commands together so let's do that right now to clear the screen here and we will do alias and I'm just going to call this command a time date okay and that alias is going to equal single quotes date and then the cow command which will print a calendar and so let us define that alias so now if I put in time dates it takes us to commands and sticks them together so I can build aliases of a combination of short commands to come up with a long command like that so that's kind of how Elias has worked now we played around with this and I've created all those new aliases but when I close my terminal check this out game they go away look sorry about that they go away so I put DF in and I'm getting it in one kilobyte blocks again that's because as soon as I close the terminal I logged out of my session and the changes to my environment went away so if you want to do things like this and you want it to stick then you're going to have to edit a file somewhere so that when bash boots up it knows where to read to find all of this information the - terminal actually reads a lot of files to get that information that I showed you in print environment today we are going to focus almost exclusively on one file because this is the one that you're allowed to mess with and if you mess it up you can't trash your system you can make mistakes here things might not work right but you haven't wrecked anything and then at the end of the video I'm going to go through all of the other different files to show you what they do okay so the file that we are concerned with today is bash RC so let us take a look at bash RC and to do that I'm going to open it in a text editor and I made the print huge here so it's pretty easy to see now as we scroll down through this file you're going to see that there's a lot of stuff in here and it defines up pretty much everything about your bash session what kind of prompts you have let's see what other kind of information we have in here it's just that setting up the history right here where it's making it so that we don't put duplicates in history then we define the history size at 1,000 and then we have stuff in here about your prompt if you want to color prompt you can also change how your prompt works I actually changed how my prompt works and I'll show you that in a little bit okay so if you want to set a color prompt for instance then you would look here and there is a place where you can uncomment force color prompt yes and then it will give you a color prompt so we've looked at that before in the Bosch RC file you can also change more intense things about the prompt we're not going to focus on that today so now you'll see that we have these aliases that are defined by the distribution here and one of them is ll the one that I showed you so you already have some aliases that are defined by the distribution and they live in your batch RC file so adding things to bash RC will make it work for you you can take your aliases come down to the bottom here and then you can open this in a text editor and you can add them and I got a bunch of stuff to find down here and we'll take a look at those in just a moment or two so there's another way to add commands to the environment and I want to show you that and that is to generate a function and generating a function will give you the ability to create a command that will accept arguments see another thing about aliases is that they don't like it if you have a command that you need to put information in so let's talk about what an argument is for those of you who are new let us say that we have a command called twiddle alright and you want to twiddle a file so what you would do normally is at the terminal you would type in twiddle and then the file name this part of this is an argument right here if you have an alias command that is supposed to accept input it may or may not work because the alias is going to screw that up a little bit also what if you want to create an alias that is way more than just one command maybe it's 2 or 3 or for commands then what you're going to want to do in this case is create a function and this is going to look a little cryptic and you're probably going to say oh I can't do that let me tell you what when I took my Linux administrators exam the part that I did not do well it on was all on was scripting okay if I can do this anybody can so don't let it scare you off it's just it's just a function so let's look at what a function is and I'm going to create a function here actually I'm going to just give you the format so the first thing that you start off with is the name of the function let's do this in the fresh text editor it'd be easier to see actually okay so let us create the first part of the function here which would be the name so in this case we're just going to say name and this is the name of your command that you want to export to the environment so we're going to create a command we're not going to save it in a file anywhere of course we're going to save it in bash RC but we are not going to save it by itself as its own script so that's what kind of like functions are like little scripts that live within bigger scripts best way to think of it so the name would be the command that you're doing so if you wanted to create a command called twiddle then you would call it twiddle so there's a couple of formats for function I'm using the one that works the best for me but you can go look up functions and if you think the other format will work better for you than fine so what we have here is a couple of parens right left and then we're going to do a bracket they're the kind of curly Q bracket that has little things sticking off the end of it and then you want to go ahead and enter and you want to tab and here is where you would type in your commands right here so then you would type in you know one command then and so forth when you're done use your tab key then type in return okay and this is going to tell the system you're done these are all the commands I need to do because what a function is going to do and it executes those commands is going to open up another process it's going to open up a sub shell it's just going to run off and do it real quick and then when it's done it will return to the main shell and that's why it's cool if you want to stick a bunch of commands in here because it doesn't mess up your environment to do it okay so now that we have put in our commands then we just do this and that's what it looks like pretty simple hunter is really not much to a function it works quite easily now once we have created a function and then we read it into the system it's still not going to work because what we need to do is take that function and we need to export it into our current environment so that we can use it as a command and to do that we would do export a function and then the name of our function which is in this case name that's it that's all the code there is to it here's where the fun begins right here in the commands this is where you can do all kinds of goofiness and you can you can do whatever you needed to do here so let me show you some functions that I have defined I'm going to show you some functions and aliases that I have defined in my own bash RC file go ahead and close that without saving so let's X add new text editor in Linux Mint 18 here I'm getting used to and then - RC okay so this is my current bash file and I'm going to scroll all the way down to the bottom here and what I have done is I have put comments in here that tell me exactly what I've done not I'm calling them aliases here but you know it could be Joe's stuff whatever and then for every one of these things that I have defined I am actually putting a comment up here that tells me what it is now it might seem a little bit ridiculous why do you got to put such deep tailed comments in here because if you do this and then you do like I do and you copy your basher RC file from machine to machine the machine and you just keep it going forever then you might want to look back at this later and try and figure out well what did I do so what's the first thing that we have here I have a variable here or a function defined that I'm calling SSH in and what this does is it allows me to get into another up and running machine without having to type in the entire IP address so remember I told you that aliases don't like to take arguments well functions don't mind at all so in this case I'm using the first argument which is this variable right here and I am telling the system run this command when I type this and a number run this command take the number stick it in here and then go off and log me into a system now if I have another machine that's up I'll be able to show you how this works so I'm going to go SSH in and this machine may not be up no it is not up so let's just log in to ourselves you can do that with SSH let's do 12 ok and it's going to ask me do you really want to log into yourself and I'm gonna say no I ain't going to do it but you get the idea if that was working then that would just dump me right into the session so that's the first one that I have defined there and the second one that I have defined is SSH in X which just means that I issue exactly the same command but this time I can use X apps through the you know X Windows applications through the terminal and have them display properly so that's X forwarding and then I have defined one here called SFTP which does exactly the same thing so if I want to fire up the SFTP program which I did a video about not too long ago then I can just type in SFTP N and then just a number it's not necessarily that it's so hard to type that IP address it's just that I keep getting it wrong what I do is I add commas where the periods should be and then that messes me up so let's get into something a little bit deeper here okay and this is this function to automate the update process you may recall a long time ago that I did a video where I showed you this particular script and this is pin a long time ago and this is a very advanced little script to run updates on a boom to systems so it could be Linux Mint could be a boon - it really doesn't matter this thing does all kinds of crazy stuff it starts the updates it goes and grabs the information and it also dumps everything into a log file that I had in my home folder so that's what all this extra code at the end here is for and then because this was a script it was defined as a command and so therefore I would throw that into like slash user slash local slash bin and then it would become system-wide all that crazy stuff well that's fine but if you switch from one machine to the next then keeping up with all of your little scripts that you got to dump in and restore it can be a pain if you can define it here in this one file if you can get the same functionality with just a function and all you have to do is backup one file then why bother you know what I mean so that's exactly what this one does right here so it's just a function and I have defined this as being update and then these are the commands that run and then that's it and then we export the update function to the environment so there you get a basic idea of how functions work and then another thing that I wanted to point out here was this alias and it's pretty much the only alias that I have defined here and this one actually links to a program that I already use so let's take a look at that particular script so this is the script a little program that I wrote to sync files between all my machines so essentially if I'm sitting at this machine which is my main machine I want to sync all the files to the other two workstations so if I make a change here it's going to show up there it's a way of backing up plus if this machine dies and I got to jump to another computer as long as I've done this recently that all my stuff should be there so that's what makes it cool now you see how much how many commands we have right this is not a terribly long script at all no but not by any stretch of the imagination - scripts can be really long this is a bit long to put in a function though if you're going to be doing more than 10-15 you know 10 lines then you don't want it in a function although you know I have been toying with the idea of taking this and sticking it in there and running this as a function because then it would be easier to deal with if I have to edit the functions that have to do with my own network C because as is this works great as long as I have a router and I'm using the 192.168 network and it has follows this exact format if my router dies are get a new one or I decide to do something different then these commands that are in here may not work so this is not written in stone if I have to go back and edit this or change it in some way then I kind of like to make that easy but for now I'm keeping the little synced program over in its own file so what this alias does here this one right here is it just runs thinking twice so I have called it sync all and it will run think it and then it will run 14 which is my HP workstation that's in the backroom and then 17 which is another laptop and as long as both of these machines are up and running and that happens all the time because my kids are on one and Cindy's on another this happens all the time I can just run this command and then quietly in the background without anybody knowing it our sync will take off and it will sync all my files up and nobody knows the better and I can also do the same thing with SSH in and update because I have export of dispatch RC file to all of my machines so no matter where I login from I'm going to get the same behavior which is really cool okay so let's talk a little bit now about files that's the basic concept the sky is the limit of what you can do in here you can change this and I encourage you to go off and look at that but there's some things that I want to clear up for new users and for some of the more advanced users who just haven't messed around with this stuff where does all this information come from that's the first thing we want to talk about actually before I even get to that I want to go ahead and make sure that I put this in there if you're going to mess around with your bash RC file then make sure that you create a backup copy before you do that and it's really super easy to do so CP dot dash R and then dot dash R and we'll give it to an extension like bak you can put anything on it you want it sold back we'll just make sure that you make a copy of it and if you turn it loose now you're going to have a copy that's there so if you mess up your bash RC file then you can go back and fix it I don't know whether I said that already in the video so I wanted to go ahead and get that out there ok so let's talk about the files where all this comes from but I also want to clear up a little mystery if you watched one of my recent videos about Linux Mint 18.1 I was sort of scratching my head because I didn't have a bunch of settings in the terminal and I thought what have they done and then I have no bash RC file nothing's working and I don't get it well I figured out what was going on with that as well so the first place and one of the first places that the system looks is in the etc' directory let's switch over to that well let's actually put the command in how's that there you go he'll work great ok so the first file that we want to look at is the profile command or the profile file we were doing I don't know cold medicines kick in a camp thing ok so this is the profile and this lives in the slash Etsy or et Cie terminal so this file is read every time anybody any user on the system a opens up a Bosch terminal so it sets down some pretty basic commands and the first thing that it's going to tell it to do is go look for a file called bash bash RC if it already exists that's what this bunch of code right here does it goes and looks for that file and if it exists and it loads stuff from there so that's all profile does in the EPC directory so let's go ahead and look at the master bash RC files that would be bash bash RC now this is the bash RC file with which defines a default environment for any user that opens up the system so if you do not have a bash RC bash RC in your home folder this is what's going to get read that's what happened to me the other day when I opened up Linux Mint 18.1 and I didn't have any colors in my terminal and there was a bunch of stuff that wasn't there and I was sitting there going what's going on linux mint 17.3 doesn't create a dot bash RC file for users if you want to create one for yourself knock yourself out open the text editor call it bash RC add your aliases your functions your changes whatever you want to do and it will work just fine but the Linux Mint 18 does but what I did was is I did an in-place upgrade which meant that when I did that upgrade the installer saw a there's already a bash RC file here and it didn't change it so it just got Corning up in the install now if you change anything in this file here anything at all this is going to be system-wide so if you add for instance an alias down here to the bottom or you add something like scream fetch which is a program that draws pretty ASCII art and gives you some information when you open the terminal if you put it here it's going to happen for every user on the system the bash RC will not negate anything that is defined in this file that isn't defined in the dot - RC file and it's okay to have duplicates - so if you had an alias defined here it was the same alias and then you had it in the other system doesn't care just ignores them because I already got that thank you very much so that's how this works when it finds these files so main thing to remember is about this particular file which is - dot - R see is that this is system-wide now if you are on Linux and you notice that I don't you don't have one let's say that you're on Linux Mint 18 you did an upgrade like I did and you want to know where you can get a template maybe you don't want to start out with a blank page because you're just learning all this stuff it'd be nice to have this stuff already here so you can poke at it then what you can do is just take a jump to another directory here called skell which is in Etsy it's your skeleton directory and this is stuff that is predefined by the distribution so let's do a LS and look for bash RC and it is there so let's actually look at the default bash RC file this is the one I didn't get because of the way I did my upgrade the other day and onto atom one all I would have had to done was just throw it in there but what I ended up doing was was adding my edited file which I do all the time anyway so it worked out in the end what am i doing I don't know you're talking and typing at the same time you can't do that stuff okay so this is the one from the Skell directory that's where we're at here and so this is going to be the basic one that you're going to get when you install Linux Mint boom - already does this most distributions provide a bash RC so there you go okay so if you need to get a template to work with you can always copy this file over and you can start hacking on it and there you go and before we close out this video let me show you one more little change that I made to my system I'm not going to show you how I did it if you want to do it you can go do the research and you figure out how to do it yourself I mean like that one of the things I changed and I did this not too long ago what is that by default the boon to and many other distributions give you a prompt that when you go into directories it will show the full path so I find knowing what user I am and what machine I'm on to be very useful and I do like to have to be colorized and then it shows you the present working directory and like I said usually what a boon to does in those situations is if you go into a deeper directory it'll show you everywhere that you are so I'll give you a for instance here let's go to documents ok so I'm going to go into that and now you see that I'm switched to documents which is my present working directory well I happen to have a directory in here that is really long it's got a long name I got several directories with long names and so therefore I would like the system not to show the documents directory in front of this here just show me the working directory that I'm in now if I get confused and I'm like well where am I at you know maybe I'm in some other user account or something there's always good old PWD and that will tell me exactly where I am so I can type that in at any given time so what you can do is jump online and you can look up customizing your bash prompt and then you'll find out what all the little goodies are in there but I will show you one thing about it if you're going to do it on a boom - or Linux Mint because figuring out where to do it can be a bit of a pain so let me just get out of that directory and then we are going to exit into - Orsi ok I want to show you where this information is kept remember we talked about the color prompt well you need to come down here and find out where your prompt is defined to change this this is your prompt ladies and gentlemen right here if color prompt yes then this is the one that counts if you have color set in your batch RC file so changes that you make is see all this stuff in here just this line just goes on and on and on when really there's only a few things that you might want to change now a lot of this has to do when you're using like Debian chroot which allows you to change your root directory and it tells the system what to do in those cases what kind of prompt to give you so be careful with this but you don't mess any of that up or you if you don't care you can always just define the prompt as being something simple but so you know like to make the change that I made yeah I'll go ahead and tell you I can't be that way see here where it says this has this capital W well the standard way to do that is a small W and if you change that to a capital W both here and here now because these are your two prompt commands depending on what terminals you're using you change them both here and in here and then it will do that so there you go I told you but go look up prompts anyway okay thanks for watching the video gang and I hope you found it very informative like I said at the beginning this is not a definitive guide this is an introduction to concepts there's a lot more to this did I mention that you can create alias files I think I did so if you create a file and call it bash underscore aliases you can have lists and lists and lists of aliases you can have the entire system doing something different than it ordinarily would do like if you wanted to and it should pick up on it because in the bash RC file that's one of the things it looks for is a file called dot dash underscore aliases and if it finds that file that's where it loads your aliases from so if aliases there something really turned you on you can do that just make sure I mention that I think I did and now that I'm repeating myself I will stop do check out easy Linux on Facebook and if you do give it a like check out easy Linux on the web we've got some new stuff up there we got wallpapers ladies and gentlemen people always asking for wallpapers well it's on the website now so go to easy Linux comm and go grab you some of the wallpaper that you see in these videos also check out freedom penguin comm I've got a glowing review of Linux Mint 18.1 there that was just published not too long ago you can read that and we will be doing this again soon thank you so much for watching and hopefully next time I record a video I won't sound so much like a troll and it won't have so much of a cold
Info
Channel: Joe Collins
Views: 57,625
Rating: 4.9365916 out of 5
Keywords: Linux, BASH, OS, Computer, Terminal, Desktop, Laptop, Software
Id: y_vAFd7Uk5U
Channel Id: undefined
Length: 35min 30sec (2130 seconds)
Published: Tue Feb 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.