DEV Community

Cover image for Paths & Comments in Bash
Jatin Sharma
Jatin Sharma

Posted on • Updated on • Originally published at j471n.in

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.

Top comments (0)