Open In App

Bash Scripting – How to Run Bash Scripting in Terminal

Last Updated : 28 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to run bash script in terminal. For example, we create a bash file set of instructions or commands ( known as bash script ), and through command, we can run this file in terminal.

Let see how to run bash script in terminal.

Example 1 : 

In this example we print or echo some text on screen with the help of a terminal.

$ echo "Geeks For Geeks"

Output : 

Print the text using echo command

Note : ” echo” this command will display the text on screen.

Example 2 :

In this example, we print the date of command execution with the help of terminal.

$ date

Print date using date command in terminal

Example 3 :

In this example, we will see that how to run a bash script file through a terminal. First, create a bash file named ” welcome.sh ” by the following command.

$ touch welcome.sh

Here, you can see that the file name is ” welcome.sh “. Whenever you create a bash script file use “.sh ” extension.

Note : ” touch “ is used to create file.

Now to write bash script, we should open this file in text editor using terminal.

$ nano welcome.sh

Note :nano ” is used to open file in text editor ( i.e. GNU nano )

Now, the text editor is opened. Write the following bash script into ” welcome.sh “

#!/bin/sh

echo "Enter Your Name : "

read name

echo "Hello $name ! Welcome to our Geeks for Geeks site." 

Note : Where,

  • #! : It is known as shebang.
  • /bin/sh : It is executable path of the system shell.
  • read : It will read the data from user.

Now, save and run this file using terminal.

$ chmod +x ./welcome.sh
$ ./welcome.sh

Note : Where,

  • chmod +x : It is used to make file executable.
  • ./welcome : Here, “./” will run the bash script file.

Output : 

Output Of above bash script

Here, we can see clearly that how to run bash script in terminal.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads