Paths & Comments in Bash
Tags: beginners, tutorial, programming, computerscience
Published: 2022-05-09 06:35:46

## Table of Contents

- [What is Path?](#what-is-path)
  - [Absolute Path](#absolute-path)
  - [Relative Path](#relative-path)
- [Commands](#commands)
- [Bash Comments](#bash-comments)
  - [Single Line Comments](#single-line-comments)
  - [Multiline Comments](#multiline-comments)
    - [Method - 1](#method---1)
    - [Method - 2](#method---2)

# What is Path?

A **path** to a file is to determine the unique location of a file or directory in an OS filesystem. You can travel or change the location of the current directory through a terminal or command line.

In file system, there are two types of paths by which you can locate the directory or a file.

- Absolute Path
- Relative Path

## Absolute Path

An **Absolute Path** is the full path of the file or directory from root to the file. An Absolute path of any directory always starts with a slash (/) representing the directory root.


```bash 
/home/jatinsharma/Desktop/Bash 
```

Now if you are in the some directory `x` and you want to determine the absolute path of that you just run `pwd` command which is stands for **Print work directory**. It gives you the absolute path of the current directory as shown below-

![pwd](https://i.imgur.com/LdcwcVi.gif)

## Relative Path

The **Relative Path** of a file is its location relative to the current working directory. It never starts with a slash (/) or root. It begins with the current work directory.


```bash 
Desktop/Bash 
```

# Commands

- `cd .` refers to the currect directory
- `cd ..` moves the directory back one directory
- `cd ~ ` this send you to the /root directory
- `cd ../..` this sends you the back by two directory
- `pwd` prints the absolute path of the current directory
  ![demo](https://i.imgur.com/5uj6QIV.gif)

# Bash Comments

It does not matter which language you are using comments are one of the essential things. In our case, you can also create comments in the bash. There are two types of comments-

- Single Line Comments
- Multiline Comments

## Single Line Comments

To write single line comment in bash, we have to use hash `#` symbol at the starting of the comment. Following is an example of Bash Script which contains single-line comments in between commands:


```bash 
# single-line-comment.sh
# !/bin/bash

# This is a single-line comment in Bash Script.

echo "hello world"

# echo output, its also a single line comment

echo "hiii, good morning"

# This is another single line comment
```

## Multiline Comments

In bash there are two ways to write multiline comments. You can use any of them according to your preference.

### Method - 1

```bash
# multiline-comment.sh
#!/bin/bash

<<COMMENTS
    This is the first comment
    This is the second comment
    This is the example of multiline comments
    This is not going to print on screen
COMMENTS

echo "Hello World"
```

### Method - 2

```bash 
# multiline-comment.sh
#!/bin/bash

:'
This is the first comment
This is the second comment
This is also the example of multiline comments
'
echo "Hello World"
```

## Wrapping up

In this article I've discussed about the Absolute and Relative paths and how you can use them and also how you can write the Single and Multiline comments in the bash script.