A kernel is the brain of the operating system that controls everything in the system. To protect the kernel from direct user interaction, there is an outer wrap called Shell. Shell is a unique program that provides the user an interface to interact with kernel accepting human-readable commands and then converts it to kernel understandable language. Shell, in a Linux operating system, can take input from the user in the form of commands, processes it, and then displays an output.
You can access Shell using Terminal in Linux. A shell can be accessed by a user using command-line interfaces. We have programs like the terminal in Linux or Mac and Command Prompt in Windows to get input in the form of human-readable commands and then display output in the same command-line interface.
Graphical shell provides users a Graphical User Interface GUI to interact, perform operations like opening, closing, saving files.
Still, behind every action, there is a shell command that executes to perform these actions. It is an updated version of the earlier Bourne shell. If you are a Linux system administrator or a power user, you must have excellent knowledge of BASH shell commands to perform day to day tasks. Mostly we use shell commands one by one in the terminal for our everyday tasks.
Still, sometimes you have to perform complex tasks or repetitive tasks, which involves a series of commands being executed in a proper sequence. A shell can also take commands as input from a file, so to make our job easy, we can write these commands in a file and can execute them in the shell to avoid manual work. These files are called shell scripts. In this article, we will help you to get the basic idea of bash scripting.
Most of the widely used operations of bash scripting will be explained with simple scripting examples. So you can create a file helloword. Echo command is the most common and frequently used command in Linux.
It is used to print text or output in the Bash. It has many options that perform different operations. Comments are part of code but ignored by the compiler. In the bash script, any line that starts with is considered a comment. For example:. Check the below command for both single and numerous comments in a bash script. Variables are named symbols used to store values temporarily. It can be a string or numeric value that we may use at any place within the script. You can make variables and assign them values.
Variable names should be descriptive so that you can understand the purpose you created that variable. User-defined variables are those which are set by us in our script.
We can also read user input from command arguments, just like any other programming language. Loops are used in every programming language where you need to execute the same code repetitively. There are two types of loops in bash script while and for loops.
We will see each one by one. While it is used when you need to repeat the line of code an unknown number of times until it satisfies certain conditions. There must also be no spaces before the or between the! Whilst you could use a relative path for the interpreter, most of the time you are going to want to use an absolute path.
You will probably be running the script from a variety of locations so absolute is the safest and often shorter than a relative path too in this particular case. It is possible to leave out the line with the shebang and still run the script but it is unwise.
If you are at a terminal and running the Bash shell and you execute a script without a shebang then Bash will assume it is a Bash script. So this will only work assuming the user running the script is running it in a Bash shell and there are a variety of reasons why this may not be the case, which is dangerous.
Given the observations above it is best to always include the shebang! It is the most reliable and convenient approach. As we saw above, formatting for the shebang was important ie no spaces, must be on first line.
There are many areas in Bash scripts where formatting is important. Typically it involves spaces and either the presence or absence of a space can be the difference between the command working or not.
I'll point these out as we encounter them. Also get in the habit of being mindful of the presence or absence of spaces when looking at code. The main reason for this is that Bash was originally developed as an interface for Users to interact with the system and later extended to have more powerful scripting capabilities.
Many decisions regarding it's behaviour were made considering only the needs of the user and then scripting capabilities had to be worked in, later, around those decisions. People generally don't mind this however as Bash scripts are still an awesome tool for quickly and easily joining existing programs into more powerful solutions.
I have seen students spend quite a bit of time in frustration that a piece of code that looks perfectly fine isn't working. They get quite embarassed when they find out the culprit a space that either should or shouldn't be there.
You will probably make this mistake a few times yourself before it sinks in so don't worry too much but the sooner you get the hang of it the happier you will be :.
Indenting of code is another area of formatting that is important. We'll look at indenting of code in section 5 If Statements when it becomes relevant. Indenting is not required but it does make your code easier to read and make it harder to make simple errors. Education is the kindling of a flame, not the filling of a vessel.
Contact Disclaimer. Bash Scripting Tutorial - 1. What is a Bash Script? Tutorial Sections Introduction 1. Variables 3. Input 4. Arithmetic 5. If Statements 6. Loops 7. Functions 8. User Interface. Let's Dive In. Both ways are shown here. You can use echo command with various options. Some useful options are mentioned in the following example.
You can use multi line comment in bash in various ways. A simple way is shown in the following example. This following script will calculate the square of 5. In the example, while loop will iterate for 5 times. The value of count variable will increment by 1 in each step. When the value of count variable will 5 then the while loop will terminate.
The basic for loop declaration is shown in the following example. Here, for loop will iterate for 10 times and print all values of the variable, counter in single line. You can use for loop for different purposes and ways in your bash script.
You can check the following link to know more about the use of for loop. Here, one string value will be taken from the user and display the value by combining other string value. You can use if condition with single or multiple conditions. Here, 10 is assigned to the variable, n. Different types of logical conditions can be used in if statement with two or more conditions.
How you can define multiple conditions in if statement using AND logic is shown in the following example. Here, the value of n will be taken from the user.
0コメント