The Wayback Machine - https://web.archive.org/web/20240912163035/https://www.geeksforgeeks.org/bash-scripting-how-to-run-bash-scripting-in-terminal/
Open In App

Bash Scripting – How to Run Bash Scripting in Terminal

Last Updated : 28 Nov, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.



Similar Reads

Bash Scripting - Introduction to Bash and Bash Scripting
Bash is a command-line interpreter or Unix Shell and it is widely used in GNU/Linux Operating System.  It is written by Brian Jhan Fox. It is used as a default login shell for most Linux distributions. Scripting is used to automate the execution of the tasks so that humans do not need to perform them individually. Bash scripting is a great way to a
10 min read
Bash Scripting - Working of Bash Scripting
Bash Scripting is a crucial component of Linux's process automation. You can write a series of commands in a file and then execute them using scripting. A Bash script is a plain text file with a set of commands inside of it. These commands are a combination of ones we typically type on the command line ourselves (like ls or cp, for instance) and ot
6 min read
Bash Scripting - Bash Read Password without Echoing back
Generally, you can see on the login page, whenever we type a password either it show dot (â€Ē) or asterisk (*) instead of a password. They do so because to protect our system or account. In such a case, the login page read the password and in place of the password, it will show a dot or asterisk. And in some cases, a dot or asterisk also may not appe
3 min read
Bash Scripting - Write Output of Bash Command into Log File
Let there are some situations in which you have to save your output in a file ( generally called log file). Output can be user details ( username, password, Gmail, etc. ), products record ( buying or selling any goods ), or simply any kind of data that you can store in a log file. Let see how to write the output of the bash command into a log file.
4 min read
Bash Scripting - Difference between Zsh and Bash
A shell is an environment in which various commands can be executed, it provides an interface between the user and the UNIX system. Basically, a shell is a command-line interpreter which interprets the commands given by the user, it can also read the combination of such commands which is known as a shell script. The shell provides us with an enviro
2 min read
Bash Scripting - Bash Echo Command
In this article, we are going to see the echo command. The Echo command is a built-in command feature for Unix / Linux which is generally used to display the text or message on the screen. Syntax : $ echo [option][text]For Example : $ echo Geeks For GeeksOutput : Geeks For Geeks There are generally three options:-n: It does not print the trailing n
2 min read
How to Customize Bash Colors and Content in Linux Terminal Prompt
If you are using the Linux operating system, that means you use the CLI most of the time. And do more work on the terminal. By default, most Linux Operating systems provide you the bash shell. Shell provides the interface between the user and kernel and executes commands. In this article, we are going to see how to customize the bash shell prompt U
9 min read
Open and Run Python Files in the Terminal
The Linux terminal offers a powerful environment for working with Python files, providing developers with efficient ways to open, edit, and run Python scripts directly from the command line. Open and Run Python Files in the Linux TerminalIn this article, we'll explore various techniques and commands for handling Python files in the Linux terminal,
1 min read
Leap Year Program with Bash Shell Scripting in Windows
Accurately the Earth has 365.24 days per year, to correct the approximation and keep track of the Solar Calendar the idea of leap year has been introduced to Gregorian Calendar. A leap year comes once every four years. In contrast, it has 366 days with regular years, having 365 days. Mathematical approach to check:Every year is a Leap year if it is
1 min read
Bash Scripting - How to read a file line by line
In this article, we are going to see how to read a file line by line in Bash scripting. There might be instances where you want to read the contents of a file line by line using a BASH script. In this section, we will look at different ways to do just that. We will use BASH commands and tools to achieve that result. We make use of the read and cat
3 min read
Bash Scripting - How to check If File Exists
In this article, we will write a bash script to check if files exist or not. Syntax :test [expression][ expression ][[ expression ]] Here, in expression, we write parameter and file name. Let us see some parameters that can be used in the expression: - -f: It returns True if the file exists as a common ( regular ) file.-d: it returns True if direct
6 min read
Bash Scripting - How to Initialize a String
Strings are quite crucial aspects of programming and scripting. We need to store the characters in some variables. In this article, we'll see how to initialize strings in BASH. Basic String Initialization To initialize a string, you directly start with the name of the variable followed by the assignment operator(=) and the actual value of the strin
2 min read
Bash Scripting - Substring
sIn this article, we will discuss how to write a bash script to extract substring from a string. Extracting an Index-Based Substring There are various ways to obtain substring based on the index of characters in the string: Using cut commandUsing Bash substringUsing expr substr commandUsing awk commandMethod 1: Using cut command Cut command is used
4 min read
Bash Scripting - Else If Statement
In this article, we will discuss how to write a bash script for the Else If statement. Conditional statements: The statements that perform specific functions based on certain conditions are called conditional statements. In bash scripting, we have several conditional statements like IF, IF-ELSE, IF-ELSE-IF, etc. Every statement has its way of worki
3 min read
Bash Scripting - Working of Alias
BASH Alias is a shortcut to run commands using some mapping. It is a way to create some shortcut commands for repetitive and multiple tasks. We create an alias in a BASH environment in specified files. We can also incorporate BASH functions, variables, etc to make the Alias more programmatic and flexible. What is a BASH Alias? A BASH Alias is a map
7 min read
Shell Scripting - Define #!/bin/bash
A shell provides an interface to connect with the system. When we use an operating system, we indirectly interact with the shell. While using a terminal every time on any Linux distribution system, we interact with the shell. The main function of the shell is to interpret or analyze Unix commands. A shell takes commands from the user and translates
3 min read
Bash Scripting - Array
Arrays are important concepts in programming or scripting. Arrays allow us to store and retrieve elements in a list form which can be used for certain tasks. In bash, we also have arrays that help us in creating scripts in the command line for storing data in a list format. In this article, we will understand the basics of arrays in bash scripting.
7 min read
Shell Scripting - Difference between Korn Shell and Bash shell
Korn Shell: Korn Shell or KSH was developed by a person named David Korn, which attempts to integrate the features of other shells like C shell, Bourne Shell, etc. Korn Shell allows developers to generate and create new shell commands whenever it is required. Korn shell was developed a long year back as it is older than the BASH shell and it has fe
3 min read
Bash Scripting - Functions
A Bash script is a plain text file. This file contains different commands for step-by-step execution. These commands can be written directly into the command line but from a reusability perceptive it is useful to store all of the inter-related commands for a specific task in a single file. We can use that file for executing the set of commands one
6 min read
Bash Scripting - String
Bash String is a data type similar to integer or boolean. It is generally used to represent text. It is a string of characters that may also contain numbers enclosed within double or single quotes. Example: "geeksforgeeks", "Geeks for Geeks" or "23690" are strings Creating a String A basic declaration and assignment of string str1="GeeksforGeeks" s
2 min read
Shell Scripting - Bash Trap Command
The trap command in Bash executes specific code in response to the signal given to the program. It traps the signal received by the program from the OS or the user and then executes the specific code provided by the programmer. It acts like a trigger in response to a signal. Just like try-catch statements help in exception handling, trap command he
3 min read
Bash Scripting - File Extension
Bash scripting is a powerful tool for automating tasks and working with files in the command line. One important aspect of working with files in bash is understanding how to handle file extensions. In bash, a file extension is the portion of a file name that follows the last period (.) in the file name. For example, in the file name "file.txt", the
6 min read
Bash Scripting - Split String
In this article, we will discuss how to split strings in a bash script. Dividing a single string into multiple strings is called string splitting. Many programming languages have a built-in function to perform string splitting but there is no built-in function in bash to do so. There are various methods to perform split string in bash. Let's see al
4 min read
Bash Scripting - Until Loop
The Bash has three types of looping constructs namely for, while, and until. The Until loop is used to iterate over a block of commands until the required condition is false. Syntax of `Until` Loopuntil [ condition ]; do block-of-statements done Here, the flow of the above syntax will be - Check the condition.If the condition is false, then execute
5 min read
Bash Scripting - For Loop
Since BASH is a command-line language, we get some pretty feature-rich experience to leverage the programming skills to perform tasks in the terminal. We can use loops and conditional statements in BASH scripts to perform some repetitive and tricky problems in a simple programmatic way. In this article, we are going to focus on the for loop in BASH
5 min read
Bash Scripting - Case Statement
A case statement in bash scripts is used when a decision has to be made against multiple choices. In other words, it is useful when an expression has the possibility to have multiple values. This methodology can be seen as a replacement for multiple if-statements in a script. Case statements have an edge over if-statements because it improves the r
8 min read
Bash Scripting - How to check If variable is Set
In BASH, we have the ability to check if a variable is set or not. We can use a couple of arguments in the conditional statements to check whether the variable has a value.  In this article, we will see how to check if the variable has a set/empty value using certain options like -z, -v, and -n. Using the "-v" optionThe -v option allows us to check
10 min read
Bash Scripting - If Statement
Bash is an interpreter for command languages. It is a default command interpreter on most GNU/Linux systems and is widely available on various operating systems. The name is an abbreviation for Bourne-Again SHell. Scripting enables for the execution of instructions that would otherwise be executed one by one interactively.  In this article, we will
15 min read
Bash Scripting - While Loop
A while loop is a statement that iterates over a block of code till the condition specified is evaluated to false. We can use this statement or loop in our program when do not know how many times the condition is going to evaluate to true before evaluating to false.   Table of Content The Syntax of a while loop in BASH ScriptingLooping Example usin
15+ min read
Bash Script - Difference between Bash Script and Shell Script
In computer programming, a script is defined as a sequence of instructions that is executed by another program. A shell is a command-line interpreter of Linux which provides an interface between the user and the kernel system and executes a sequence of instructions called commands. A shell is capable of running a script. A script that is passed to
3 min read
Article Tags :