Let's Learn Bash! Tutorial 001

in #technology6 years ago (edited)

Let's Learn Bash!.png

This is a start to a series all about bash and scripting for the terminal. There are lots of commands to cover but today is all about the basics. As long as the information fits within the one thousand word limit that this blog adheres to we should cover what bash is and some basic commands. The goal is to get you moving around the terminal and know what a bash script is by the end of this post.

Bash is an acronym for "Bourne Again SHell" because it is the open source response to the original "Bourne Shell" used on many old UNIX systems. Brian Fox created bash while working for the Free Software Foundation in the 1980s. We can now find this scripting language on (almost) all Linux distributions and Mac OS (sorry Windows fans you'll need an emulator).

A while back we talked about the terminal and why it is an awesome tool. Bash runs in the terminal and allows the user to execute many commands via a script. This saves us a lot of time when using the terminal. Especially when we have a complex or long string of commands to run each day.

As an example, we can create a script to run as soon as the computer finishes loading. In said script we can run programs we always want running in the background. Dropbox and f.lux are great examples of programs that need to be running all the time. If we have to execute a command every time we turn on our computer, it will get old.

The Basics

As mentioned above, a bash script is also great for complex commands or a long string of commands. Let's say we want need to run the same series of git commands several times an hour. We can save time by using a bash script and writing all the commands out once within the file. This saves time upon each use since now instead of running four or five commands we only need to run one.

learn-bash-scripting.jpg

Here is an example of what a bash script looks like:

#! /bin/bash
STRING="This is a simple bash script."
echo $STRING

So what does this do? Once the script runs in the terminal, we will see "This is a simple bash script" on the next line down. This is not useful but is a good way to show the format of the script. As with all programming and scripting languages if some part of the formatting is incorrect we will either not get the results we desire or the code will not run at all.

#! /bin/bash is what tells the terminal what type of script we are asking it to run and should be a part of every bash file. STRING is a variable that holds the information between the quotations. Any time we use STRING in our file bash will interpret that as the text that follows. Finally echo, this is the command that tells the terminal to show on screen whatever follows. In our case it is the variable we mentioned earlier.

That is how we set up the bash file and add commands. When saving the file we can call it anything we want so we can remember what that script does. There is no need for an extension if you are using a Linux distribution. However, it may be useful to use .sh to the end as a reminder that the file is a shell script.

Now we will have to tell the computer to make our file executable. To do this, we need to run a command in the terminal. The command is chmod +x [file]. If the computer says we do not have permission than add sudo to the front of the command.

Also, if we can not run the script after making it executable we need to run chmod 755 [file]. This tells the computer to let us run, read, and edit the file. While everyone else can only run and read. There are many other number combination we can not get into now so search "chmod permissions" if you want a different set.

Edit: @not-a-bird brought up a good point when executing chmod 755 [file]. While using sudo on chmod +x [file] is fine, using it here will not be as effective. This is do the order of the numbers and what they represent. We did not have the space to cover that here and I should have at least let everyone know not to use sudo there. Check out his comment for more information.

learn-bash.png

Easy Shell Commands

The more we use the terminal the more we memorize these commands. They became muscle memory as many people discover after switching to a new keyboard layout such as Dvorak. Perfect practice makes perfect so we need to make sure we get all the basics correct.

First what is a command? A command in a string (like a sentence) of letters, numbers, and symbols that tell the computer to do a specific task. The most basic and most common command is cd.

This is an acronym for "change directory" and it does just that. However, we need to tell it what director we want to change to. If we do not specify a folder, the command will take us back to our home directory. The command will look as follows:

cd .. or cd Downloads/

The first command will take us up one directory. Think of this like a back button. We don't say "back one directory" because the computer's file system has a tree like structure. The second command above will take us to the "Downloads" directory. Awesome, but how do we see what is in this directory?

ls

This command stands for "list' and it will display all non-hidden files and folders our current directory. To veiw everything, including the hidden files, we need to add an argument. Arguments (almost) always follow directly after the command. If we want to see the directory's hidden files, we need to use the command "a" argument. Make sure to add - (dash) before the argument.

ls -a

Great! We can now move around the terminal. Doing so is the absolute basic step in using the terminal and creating bash scripts. The reason we need to know this is that bash needs to move around just as we would if running the commands by hand. When we create a script we are saving time later by typing the command sequence once in the file. Instead of every time we need to execute the task.

What we covered here are the fundamental blocks in building the proper bash scripting knowledge. In the next post we will cover more about the bash language as used in the executable file. The topics will focus on variables, strings, if-than statements, and a whole slew of other awesome stuff. All to make our computer do epic things fast than we ever could.

Thanks for reading!

If you have any questions please ask and I will do my best to get you the answer.

All images came from royalty and attribution free sources unless specified
Sort:  

once upon a time i could do this stuff...

Bex! What change?

I used to tech edit for a Linux company. Back then I knew and used a lot of skills I don't remember anymore. After my health became problematic, my ability to remember things faded. Not using it doesn't help. Still have one system running Linux. Swapped mine to Windows for a voice recognition system that I've never been able to get working satisfactorily (tried again today with Dragon as well as built in Win.. I just talk too fast and it frustrates me). So I've lost a lot of things. I still have to stop myself all the time from writing s/x/y and $whatever because that got so engrained. I even knew how to compile source code once.

That is awesome Bex! And also a shame there is not a good solution for voice input that you can use on Linux.

chmod -x [file]

That actually removes executable permissions from the file. You wanted "+x" instead of "-x".

We don't say "back one directory" because the computer's file system has a tree like structure.

That would have been a good time to point out that it's actually up a directory because it's a tree. Similarly it's down a directory when going into the Downloads... but I suppose this isn't terribly important.

Nice article.

After some consideration, I'd like to add a few more comments.

If the computer says we do not have permission than add sudo to the front of the command.

While this will technically work, you should probably consider whether the proper user owns the file. If the file is yours, you can change the mode to anything you want. If you use sudo you're becoming the root user before executing the command, and you should be aware that 755 means that the owner of the file will have read, write, and execute. Other users in the same group will have read and execute permissions. Everyone else on the system will have read and execute permissions. Generally this is acceptable, but if you are blindly running sudo before your chmod you should be aware of these consequences because it's an unusual situation where your user does not own the file.

I'd also like to add that even though it is a little intimidating for new users, the man page for Bash is incredibly thorough. You can check it out with man bash on your system, or online.

I'll update the chmod line now thanks for catching that.

Also thanks for the additions. I am trying to keep these at one thousand words so I appreciate the extra info in your comment. I had to pick and choose what to add in this post and where to expand deeply. Naturally things will get left out.

Yeah, I was aware of why you may have left things out, which is why I originally wasn't going to add more, but since you are aiming at beginners, I worry that they might not consider the consequence of just adding sudo to all of their commands.

But then it occurred to me, hey, this place is great for discussion, so I'd add it anyway as a sort of discussion point.

Also, the markdown on your edit is a bit off.

hahaha yeah I noticed that about the markdown when I was reading it over. But yes pleas feel free to bring up something that might not be clear as I go through this. We both know how deep this can be.

Ahhh, good old bash. I echo @bex-dk — once upon a time, this was child's play for me. I was a CS grad, had a good programming job, swam in Linux as if it were my second home.

Got burned out. Ran away from it. Switched to Mac so that I didn't have to think about any of it anymore. Oh yeah, and moved to Japan and got into photography and poetry and had some kids and drank too much sake and all that.

You wrote this tutorial very well. Brings back some memories.

Thanks for the comment and kind words. Linux is a great daily driver these days. You should try using it again ;)

Congratulations @jrswab! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of comments

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

@jrswab cool post, looking forward for the next tutorial.

Would be great if you can talk also about how to create a Makefile, from my perspective it would be really useful.

Thanks for sharing

Thanks for the comment! I'll see where I can add a make file in the coming lessons.