Bash Scripting – How to Run Bash Scripting in Terminal
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"
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.
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.
Here, we can see clearly that how to run bash script in terminal.