Paths & Comments in Bash

j471n

Jatin Sharma

Posted on May 9, 2022

Paths & Comments in Bash

Table of Contents

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.

/home/jatinsharma/Desktop/Bash 
Enter fullscreen mode Exit fullscreen mode

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

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.

Desktop/Bash 
Enter fullscreen mode Exit fullscreen mode

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

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:

# 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
Enter fullscreen mode Exit fullscreen mode

Multiline Comments

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

Method - 1

# 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"
Enter fullscreen mode Exit fullscreen mode

Method - 2

# 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"
Enter fullscreen mode Exit fullscreen mode

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.

💖 💪 🙅 🚩
j471n
Jatin Sharma

Posted on May 9, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

An overview Of The OSI Model.
beginners An overview Of The OSI Model.

August 17, 2022

Paths & Comments in Bash
beginners Paths & Comments in Bash

May 9, 2022

Create Bash Script & File Permissions
beginners Create Bash Script & File Permissions

April 22, 2022