Paths and Comments in Bash

Jatin Sharma

Jatin SharmaMay 09, 2022

3 min read516 words

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.

Example

/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

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.

Example

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

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
 

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"

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"
 

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.

Bash series

Jatin's Newsletter

Get notified in your inbox whenever I write a new blog post.

Share on Social Media: