Quantcast

Start with 7 free days of training.

Gain instant access to our entire IT training library, free for your first week.
Train anytime on your desktop, tablet, or mobile devices.

CBT Nuggets trainer Shawn Powers walks you through everything you need to know to master Linux! Shawn takes a practical approach, providing real-world examples of how to apply your open source skills and knowledge....
CBT Nuggets trainer Shawn Powers walks you through everything you need to know to master Linux! Shawn takes a practical approach, providing real-world examples of how to apply your open source skills and knowledge.

Recommended Experience
  • Basic familiarity with Linux and/or non-Linux computer usage
Recommended Equipment
  • Desktop or laptop system running Windows, OSX, or Linux
Related Certifications
  • Linux Essentials (Linux Professional Institute)
  • LPI LPIC-1 and CompTIA Linux+
Related Job Functions
  • System administrator
  • Network administrator/engineer
Shawn Powers has been a CBT Nuggets trainer since 2009 and a Linux system administrator for more than 20 years. He has been using and teaching DevOps-related technologies since the inception of the concept. He has a passion for Open Source technologies and loves teaching.

Note: Supplemental files and/or virtual labs are not available until the entire course is completed.
 show less
1. Scripting: Editors — Entry Level (7 min)
2. Scripting: Using BASH Shell Scripts — Entry Level (8 min)
3. Scripting: BASH Variables — Entry Level (8 min)
4. Scripting: BASH Conditionals — Intermediate (10 min)
5. Scripting: Nested Conditionals and Case Statements — Intermediate (11 min)
6. Scripting: BASH While and Until Loops — Intermediate (7 min)
7. Scripting: BASH FOR Loops — Intermediate (11 min)

93 additional videos are in-progress.

Scripting: Editors — Entry Level

00:00:00

No, it may seem like a silly thing to talk about, but when we're talking about bash scripting it's really important that we figure out what text editor we want to use, because bash scripts are text only. So you can either use a GUI tool or a command line interface tool, that's what CLI stands for.

00:00:16

And I really want to talk about what not to do when it comes to writing bash scripts with your editor. So the first thing we need to figure out is what is the best way to go about editing a text file. Now you've probably heard the saying, there are many ways to skin a cat.

00:00:31

Well I think that's an absolutely horrible saying, we will not talk about skinning cats. Rather, I want to go right to our command line so we can look at the various types of editors we have available to create these bash scripts that we're going to look at.

00:00:44

Now since we're using a GUI environment or a graphical user interface on top of Linux, one of the things you're able to do is use a GUI or a graphical user interface text editor. Now this one happens to have Pluma text editor installed. If we open this up, you'll see it's a GUI tool.

00:01:00

And we could open a file, like you'll notice on the desktop I have this SampleScript that I put on here. And it'll open, and you can edit it, and then save it. And it'll work just like every other program you would expect. Now it does have a few cool features, for example, syntax highlighting.

00:01:16

Now this is just plain text, but if you look it actually highlights things based on the code. So this is bash code, and it knows how to highlight things differently. So you know this is green. This is red. This is purple. That's really useful, because if you accidentally do a typo, like if you misspell a command like while, it's not going to highlight it, because it's not going to recognize it.

00:01:37

So it's going to look weird. It's just a really nice way to keep things organized, and be able to identify things quickly. If you forget this close quotation, for example, it's going to highlight things differently and that's not what we would expect.

00:01:51

So if we close that it's going to make it look right. And it's going to be highlighted just like the rest of the things. So syntax highlighting is powerful. You want to use syntax highlighting if at all possible. Regardless of whether you're using a GUI or a text based editor.

00:02:05

Now again this is perfectly acceptable, if you want to do this. I'm not going to save any changes here. You can use that GUI tool if you want. Now the one thing you don't want to do, though, is to use a GUI editor that happens to be like a word processor.

00:02:19

So for example, LibreOffice Writer is here, you do not want to edit text files with LibreOffice. Now is it possible? Sure, we could say file, open, and go to the desktop, and find our SampleScript. And it's going to be here, but a couple problems with this, one you'll notice there's no syntax highlighting.

00:02:39

It just sees this as a text file, and it wants you to edit things. Using this we could make it bold, and things like that, which is not text only. We only want it to be text, and things like this just are not compatible with bash scripts. So do not use something like LibreOffice or Microsoft Office when creating bash scripts, it's just not ideal.

00:03:00

So I'm gonna say, don't save. Now if you're using Linux chances are at some point you're going to find yourself in situation where you only have access to the terminal. For example Linux servers are often called headless, meaning they don't have this graphical user interface.

00:03:15

So if you're stuck in a position like that where you only have text, it's really important to understand text editors. So let's go to the desktop and inside here we'll see that SampleScript. Now, if you type nano, nano is the name of a very powerful text editor.

00:03:32

It's the one I recommend that you use as we edit files inside a command line window in Linux, in general. Nano is just awesome. It's really easy to use. You just type nano space the name of the file you want to edit, press Enter. You'll notice it does have syntax highlighting which is awesome.

00:03:49

We would hope that it would have syntax highlighting. And you can edit files, just simply-- like you would arrow keys and make changes. And then when you make a change and you want to save it, there's a little cheat sheet right here at the bottom. So Control X, for example is how you exit.

00:04:07

So we do that. It says, do you want to save the modified buffer. That's basically saying, do you want to save your changes. You can either do yes, no, or control-c to cancel. So I'm going to press the Y key. And then it says what file name do you want to save it as.

00:04:19

Well it's the same file name by default, and we're going to leave that and press Enter. It'll save over top of what's there. But we could actually change the name. We could say SampleScript 2, and then it would act like save as, if you're going to save it as something new we would have two things.

00:04:35

I actually want to save it right over top, so I'm going to leave the default where it says file name to write SampleScript, press Enter, and now this script has been changed. It's really cool, since we have a GUI environment, you could do something like this, if you double click on the script, it's going to say, all right so this is executable, do you want to run it in the terminal.

00:04:55

And if we do that, it's going to open a new terminal and execute the bash script. Now this is simple little bash script I made to show you the syntax highlighting and stuff, however, it does give us instructions. If you want to stop bashing, press control-c.

00:05:07

So I'll press Control c, and it'll close that window. So the GUI does give us a couple of nice things, you can double click on it and say Display, and it conveniently will then open it in Pluma. So you can make changes right inside this GUI environment.

00:05:23

So by all means, you can use the GUI tools here. But knowing how to use it on the command line is important. I'm going to do everything inside the command line. And I'm actually going to use an editor called vi. Now I don't recommend that you use vi, only because while it does support syntax highlighting, it's a really complicated editor to get the hang of.

00:05:43

The only reason I use it is because I've been using it for over 20 years. Things like nano didn't used to exist. And so I had to learn to use vi and now my fingers and brain kind of just go on autopilot, because in order to edit you have to do really bizarre things.

00:05:57

Like go to this. And then you have to go into Insert mode in order to make changes. And then you have to press Escape to get out of Insert mode. If you want to save it, you have to type colon wq, which is not intuitive, and there's no cheat sheet. But this is the editor that I'm going to use as I show you how to do things.

00:06:18

But again you can follow along and do everything I'm doing in vi, but use nano. And it's going to be a lot easier, and it's really what I recommend you use. I could use nano but again vi just comes naturally. If you want to play with vi, by all means, you can do that.

00:06:33

It's going to be installed on every Linux and Unix box you log into. Vi is going to be an option. However nano is going to be there almost as often, so I really recommend using nano. Regardless of what editor you use it's important to remember that it has to be text only.

00:06:49

So do not use a word processor as we're editing these bash scripts. So while I don't agree that multiple ways to skin a cat is a good idea, I do agree that it is important to understand there are multiple ways that we can edit text files. And not one is correct, it's just that it's going to be personal preference.

00:07:08

Although I do recommend that you do your best to learn how to use a command line interface text editor, because with those headless servers you're not going to have a GUI option. So if you rely on a graphical user interface you're going to be struggling when it comes to servers that don't have a GUI.

00:07:24

So with this lab I urge you to play around with the different text editors that are here, get comfortable with one, and whatever one you pick just use that as we go along and learn how to do bash scripting in other Nuggets. Again text is text, it doesn't matter if you do the exact same thing I do it's just important that the end result is the same.

00:07:42

I hope this has been informative for you, and I'd like to thank you for viewing.

Scripting: Using BASH Shell Scripts — Entry Level

Scripting: BASH Variables — Entry Level

Scripting: BASH Conditionals — Intermediate

Scripting: Nested Conditionals and Case Statements — Intermediate

Scripting: BASH While and Until Loops — Intermediate

Scripting: BASH FOR Loops — Intermediate

Please help us improve by sharing your feedback on training courses and videos. For customer service questions, please contact our support team. The views expressed in comments reflect those of the author and not of CBT Nuggets. We reserve the right to remove comments that do not adhere to our community standards.

comments powered by Disqus

Training Features


Practice Exams
These practice tests help you review your knowledge and prepare you for exams.

Virtual Lab
Use a virtual environment to reinforce what you are learning and get hands-on experience.

Offline Training
Our iOS and Android mobile apps offer the ability to download videos and train anytime, anywhere offline.

Accountability Coaching
Develop and maintain a study plan with one-to-one assistance from coaches.

Supplemental Files
Files/materials that supplement the video training.

Speed Control
Play videos at a faster or slower pace.

Bookmarks
Included in this course
Pick up where you left off watching a video.

Notes
Included in this course
Jot down information to refer back to at a later time.

Closed Captions
Follow what the trainers are saying with ease.
Shawn Powers
Nugget trainer since 2009