en
  English
Phone :  +370 (5) 204-1903
Email: sales@1gbits.com
  1. Dedicated server
  2. Blog
  3. What is Bash? An introductory guide!

What is Bash? An introductory guide!

The Free Software Foundation birthed bash or Bourne-Again shell under the GNU project. At present, when you see most of the Linux installations, bash comes out as the default user shell. Although there are a lot of other UNIX shells available, bash is widely distributed with Linux, and hence, it becomes an important tool.

Linux Tutorial May 12, 21 by Nisal N 9 min Read
What is Bash? An introductory guide!

The Free Software Foundation birthed bash or Bourne-Again shell under the GNU project. At present, when you see most of the Linux installations, bash comes out as the default user shell. Although there are a lot of other UNIX shells available, bash is widely distributed with Linux, and hence, it becomes an important tool.

Bash is also a successor for two shells, namely, the Korn shell and the C shell. Let us analyse the topic in detail.

What is Bash?

Bash is a UNIX shell, as well as a command-line interpreter. It is also considered a frequently used programming language. In short, bash performs two significant roles, both at the same time. 

It supports various functions, variables, loops and conditional statements similar to several other programming languages. The user can interpret commands to execute several actions with its help. 

There are great features associated with bash. You can perform various tasks on your system. Some of them are enlisted here:

  • Edit your files
  • Fetch web pages from the web
  • Adjust the volume of the OS
  • Automate your work
  • Command-line editing

Let us tread ahead and understand the usage of bash on the command line.

Usage of bash on the command line (Linux, OS X)

To use bash on either Linux or OS X, open the terminal and write the commands you want to execute. For instance, take a look at the below-mentioned command:

echo “Hello world.”

This command gives you the output as the ‘hello world’ string.

Hello World 

This way, you can write several commands and similarly execute them. Moving on, here is an explanation of the process to write a bash script.

How to write a bash script?

As you have seen, you can write bash commands and execute the same. Further, you can write all the bash commands in one .sh file and run that file afterwards. 

For instance, if you have to insert the following content in the file, use:

#!bin/bash

echo “Hello World”

A little information about the syntax:

As you can see, the command begins with a #! symbol. This symbol is also known as a hash-bang, Sha-bang or she-bang. It plays a vital role to make the interactive shell understand the interpreter needed to run the program. 

After the initial symbols, the first line tells UNIX that bin/bash will execute the file. Moreover, this location is known to be the standard location on every UNIX system. 

Additionally, the first line of the script informs the operating system to invoke the shell to execute the mentioned commands. 

Moving on, you can also run the script as mentioned below using the following syntax:

$ ./myBashScript.sh

Hello World

But sometimes, there occurs an error message in front of your screen. It happens due to some of the permissions set on the file. To overcome this obstacle, type the following command:

$ chmod u+x myBashScript.sh

Press enter to execute. Your problem should be resolved now. Let’s move towards the next segment. 

Here are some of the essential commands used in bash. 

Bash Cat

Cat is very commonly used in a Unix operating system. The purpose of cat command is to scan the file in a sequence and print the output. Cat is the short form taken from its function to concatenate files. It is used as follows:

cat [options] [file_names]

Below-mentioned are a few of the most frequently used options:

  • -b, gives a total number of non-blank output lines.
  • -n, gives a total number of output lines.
  • -s, squeeze adjacent blank lines.

Example:

To print the content of file.txt in the terminal, type the following command in the terminal window:

cat file.txt

To concatenate the content of two files and display the result in the terminal, use the below-mentioned command:

cat file1.txt file2.txt 

Bash cd

cd stands for Change Directory. The syntax for this command is given as follows:

cd [OPTIONS] directory

This command accepts two options that are given below:

  • -L: It follows the symbolic links. By default, cd behaves according to this command even if it is not mentioned. 
  • -P: It doesn’t follow the symbolic links. 

Moreover, if you wish to check the name of your current directory, use the pwd command.

Now, your current working directory is, by default, the home directory. Say, for instance, the Downloads directory resides in the home directory, to access the same, use the following path:

cd Downloads

Additionally, you can also follow the absolute path to access the directory. Type:

cd /home/username/Downloads

Whenever you encounter the / symbol, consider the path as the absolute path to a directory.

Moving on, always remember that your current working directly shows a single dot, ‘.’. At the same time, two dots represent the directory just above the current one. 

Take a look.

Suppose, your current directory is /usr/local/share. If you wish to jump to the immediate above located directory, /usr/local/, type the following:

cd ../

Now, follow the same pattern to reach the /usr/ directory. 

cd ../ src 

In short, here are some of the important symbols that help you to write commands. Take a look.

  • “.” indicates the current directory. For instance, ./projects 
  • “/“symbolises the root of the system wherein you can navigate to the core folders like user, system, etc.
  • “..” helps you to jump onto the previous folder. Additionally, you can combine it to go back to multiple levels by using ../../my_folder
  • “~” Who is known to be the home directory.

Bash man

Man command, abbreviated as manual, is a bash command which provides the user manuals for the respective commands.

Take a look at the syntax mentioned below:

man [options] [command]

Some of the most used options:

  • -f: Displays a brief description of the manual page
  • -a: Displays all the introduction manuals pages in the manual in a sequence.
  • -C file: You can use this user configuration file instead of ~/.manpath, the default file.
  • -d: It prints the debugging information 
  • -k: It searches for the short manual page descriptions for some keywords and showcases the matches if any
  • -c: This is not generally used, but only used by the Catman program
  • -l: Performs a search for the manual pages considering case-sensitivity 
  • -h: It prints a help message 
  • -V: Helps to display the information about the version

You can use these options to carry out the implementations as per your requirement.

Bash mv

The mv command is considered when you wish to move files, directories, or folders from one location to another. It can be used to move single files, multiple files or directories too.

Remember the following pointers while working with the mv command:

  • When the Source is multiple files or directories, the Destination has to be a directory. 
  • If you mention a single file as the Source and a directory as the Destination, the file moves to the target directory.
  • In case the Source is a directory, and there is no Destination, or the Destination doesn’t exist, the Source becomes the Destination.

To move a file using the mv command, the file to be moved is written before the file it is moved to. Take a look at the syntax:

mv [OPTIONS] SOURCE DESTINATION

Here is an implementation:

mv file1.txt file2.txt

Here, file1.txt is moved to file2.txt.

Moreover, if you want to move the pdf files to a particular directory, type the given stated command:

mv *.pdf ~/Documents

Now, you can also create a backup of a file by using the -b option. Take a look:

mv -b file1 /tmp

To verify that the backup is created, use the Is command:

ls /tmp/file1*

The output is as follows:

/tmp/file1 /tmp/file1~

Also, there are situations when some files are overwritten. To avoid this circumstance, use the -n option. Type the following command:

mv -f file1 /tmp 

Additionally, if you want to move any file or directory, you must possess write permissions on both the files, namely, the Source file as well as the destination file. Otherwise, the system displays an error message. 

Some commonly used options:

  • -f: It is used to forcefully move a file and overwrite files without the user’s permission.
  • -i: It is used to receive a confirmation before overwriting files.

Now, the following section tells you about the advantages of bash.

Advantages of Bash - Finale

Bash is able to run most of the sh scripts without any specific modifications. The primary objectives behind its creation were to protect, copy, modify, or redistribute the software.

There are tons of pros related to bash. Here is a list:

  • It is effortless to use.
  • Bash is portable.
  • You can run multiple commands as a single command.
  • Frequently used operations can be automated.

With this, we have reached the end of this article. We hope that the information assists you in developing a better understanding of the topic. 

author img

Nisal N

Computers has always fascinated me since I was a kid and here we are. I love travelling for 2 reasons: the first one to see a new part of the world and second (the most important one) to experience the rich culture hidden among the country and people. I'm pretty good at cooking but very poor when it comes to baking.

Leave A Comment