Relative vs. Absolute path
Before generating an original Bash Script, you ought to be aware of the shell exploration and the difference between the relative and absolute path for an expected document or script. So let's learn what they are?
What is the Path?
A path to a file may be a merged sort of slash (/) and alpha-numeric characters. It defines the unique position of a repository or document in an OS filesystem.
Absolute Path
An Absolute Path may be a full path specifying the situation of a file or directory from the basis directory or start of the particular filesystem.
Example: /home/Lenovo/Desktop/CollegeStudent
An Absolute path of any directory always starts with a slash (/) representing the directory root. Besides this, all slashes within the directory path separate the directories.
All directories names within the absolute path are written within the hierarchy order. The origin repository name is written on the left.
The surname in an absolute path may belong to a file or directory. Besides the Username , all names belong to the directories.
The absolute path of the present directory is often determined by using the pwd command.
Relative Path
The relative path of a file is its location relative to the present working directory. It never starts with a slash (/). It begins with the continued work directory.
Example: Desktop/CollegeStudent
Single Dot (.) resolves to the present directory.
Double Dot (..) resolves to the parent directory of this work directory.
Tilde (~) represents the house directory of the logged-in users.
Relative Path Vs Absolute Path:
The topmost directory in any filesystem is that the root directory denoted by the slash (/). you'll describe the situation of any file or directory within the filesystem with absolutely the path. meaning you'll take every step ranging from the basis directory or absolutely the beginning of the filesystem.
An absolute path is unambiguous and should be inconvenient to figure with, especially if you're working with deeply nested directories. to urge an easier mode of things, you'll use the Relative path instead. Such that, if you're working with files in or near this working directory, this will prevent from tons of typing.
Every time you've mentioned a file by just its name, you have been employing a relative path. this is often the foremost straightforward kind of relative path. The shell looks for the required file name within the present directory.
We can navigate in the shell using two commands,
- pwd (It displays the present working directory)
- cd (It changes the directory)
- ls (it list outs the files present in the pwd).
Bash Comments
In this topic, we'll understand the way to insert comments during a Bash Script file.
Comments are a necessary a part of any programing language. They're utilized to define the usage of any code or function. Comments are the strings that help within the readability of a program. they're not executed once we execute the commands within the Bash Script file.
Bash script provides support for 2 sorts of comments, a bit like the opposite programing language.
Single Line Comment
Multiple/ Multi-Line Comment
Bash Single Line Comment
To write one line comment in bash, we've to use the hash (#) symbol at the starting of the comment. Following is an example of Bash Script which contains single-line comments in between commands:
#! /bin/bash
Bash Multi Line Comment
There are two methods to insert multi-line comments in a bash shell scripts:
- We can write a multi-line comment in bash scripting by inserting the comments between <<COMMENT and COMMENT
- We can also use a multi-line comment by adding the comments between (: ‘) and (‘)
Metho 1:
handy if we use it nicely :) "
Here You can see that even I modified the a.sh file with comments but still the output is the same as before.
Method 2:
Output: