How to Run Bash Script in Github Actions ?
Last Updated :
30 Apr, 2024
GitHub Actions are helpful resources for coding. They automate processes in your GitHub projects, save you time and effort. It is possible that GitHub Actions will automate the steps involved in testing, deploying, and alerting users of your code. Because you can configure them to run at specified times, like when you complete code to your repository or receive a new issue, they are quite helpful. In simple terms, GitHub Actions automate tedious tasks inside your GitHub projects to streamline your workflow.
Purpose of Bash Scripts
Bash scripts are required for the running of GitHub Actions processes because they enable a variety of job automation. They function as collections of Bash scripting commands that outline GitHub Actions' appropriate course of action.
You can modify actions with Bash scripts to meet your own requirements. To compile your code, execute tests, or launch your application, for instance, you can construct a Bash script. These scripts provide you the freedom to customize actions to your exact specifications, making it simple to automate intricate procedures.
Put it simply, Bash scripts enable you to design unique actions in GitHub Actions, which facilitates task automation and simplifies the development process.
Step by step to run Bash script in Github Actions
Step 1: Create the repository on GitHub
Step 2: Here is the Created repository named the bash-script for your reference refer to the below image.

Step 3: We need to create the action file under the work flows. Here we have created the blank.yml. The bash script is stored on bash.sh. For clear guidance refer the above image.
For better understanding here is the action file and bash script.
name: Bash Script
on:
workflow_dispatch:
jobs:
bash-script:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Run Bash script
run: bash bash.sh
- name: Identifies the workflow by name. In this instance, it is "Bash Script," which is merely a label for identification and has no bearing on how it works.
- on: Specifies the time at which the workflow should start. The process is manually triggered via the workflow_dispatch event, which means that a user must start it.
- jobs: Includes one or more tasks that need to be completed as part of the workflow.
- bash-script: Describes a work called "bash-script." The latest virtual machine environment for Ubuntu is being used for this job.
- runs-on: outlines the kind of hardware that should be used for the project. It is an Ubuntu latest environment in this instance.
- steps: includes a set of actions that must be taken in order to complete the task.
- Checkout code: checkouts the repository's code using the actions/checkout@v2 action. This stage makes ensuring that the code is usable in the steps that follow.
- Run Bash script: Carries out a Bash script called bash.sh that is found in the root directory of the repository. The script is executed using the run keyword. Here, it executes the name of the Bash script first, then the bash command.
#!/bin/bash
# Print current directory
echo "Current directory: $(pwd)"
# List files in the current directory
echo "Files in current directory:"
ls
# Display the current date and time
echo "Current date and time: $(date)"
Shebang line: #!/bin/bash
- The term "shebang" refers to this line, which instructs the Bash shell (/bin/bash) to read the script. The script's interpreter needs to be specified.
Print current directory:
echo "Current directory: $(pwd)"
- The
echo
command is used to print text to the terminal. $(pwd)
is a command substitution. It executes the pwd
command, which stands for "print working directory", and substitutes the output (the current directory path) into the echo command.- This line prints the current directory to the terminal.
List files in the current directory:
echo "Files in current directory:"
ls
- The
echo
command is used to print the text "Files in current directory:" to the terminal. - The
ls
command lists the files in the current directory. - This pair of commands prints a list of files in the current directory to the terminal.
Display current date and time:
echo "Current date and time: $(date)"
- The
echo
command is used to print text to the terminal. $(date)
is another command substitution. It executes the date
command, which displays the current date and time, and substitutes the output into the echo command.- This line prints the current date and time to the terminal.
Step 4: Click on actions of the repository. Here the cicd successfully completed in the github actions refer the below image then click om=n the bash script.

Step 5: For the console output of the repository click on the bash-script job you will find the clear console output of the cicd.

Step 6: The all jobs are successfully completed in the repository.

Step 7: Here refer the below image the script was successfully executed refer the below image.

Conclusion
Bash scripts allow for more freedom and customisation, whereas GitHub Actions automates operations to streamline project workflows. We demonstrated how to use Bash scripts in GitHub Actions with a step-by-step tutorial. Developers can improve software quality and efficiency by focusing on core development tasks, ensuring consistency, and increasing productivity by integrating these techniques.
Similar Reads
How to Run Python Script in GitHub Actions ?
A tool available on GitHub that can help you automate chores in your software projects is called GitHub Actions. It enables you to design workflows that, when executed automatically, carry out actions like as deploying, testing, and even sending out notifications. It basically works like a small rob
6 min read
How to Skip a Job in GitHub Actions ?
With the help of GitHub Actions, developers can effectively automate a range of processes and workflows right within their repositories. You can develop, test, and publish your code using GitHub Actions without ever leaving the GitHub website. It provides an adaptable and configurable workflow frame
6 min read
How to Add GitHub Actions Secrets ?
When it comes to safely managing sensitive data in your workflowsâlike access tokens, API keys, and other credentialsâGitHub Actions secrets are essential. By using these tricks, you can securely access and save private information without exposing it to the source code of your repository. You may i
5 min read
How To Set Up Merge Queues in GitHub Actions
Managing pull requests (PRs) efficiently in a collaborative development environment is crucial to ensure that the codebase remains stable and conflict-free. With GitHub Actions, automating the PR merging process with merge queues can help streamline your workflow, reduce manual intervention, and ens
6 min read
How to use AWS CLI in GitHub Actions ?
Through a command-line interface, Amazon offers a powerful tool called the Amazon Web Services Command Line Interface, or AWS CLI, which makes managing AWS resources easier. The importance of this is that it can simplify the process of utilizing AWS services straight from the terminal, removing the
4 min read
How to Test GitHub Actions Before Merge?
GitHub Actions work as minor helpers, take care of tasks automatically whenever something goes wrong in your GitHub project. GitHub Actions could be likened as an ever-present friend who is ready to help anytime you need it. Consider how nice it would be to have your code automatically tested follow
6 min read
How To Run Bash Script In Linux?
Bash scripts, also known as shell scripts, are powerful tools in the world of command-line automation. They allow you to perform a series of tasks or execute commands by writing scripts that can be run in a terminal or command-line interface. However, the question often arises: how do you run a Bash
6 min read
How to run bash script in Python?
If you are using any major operating system, you are indirectly interacting with bash. If you are running Ubuntu, Linux Mint, or any other Linux distribution, you are interacting with bash every time you use the terminal. Suppose you have written your bash script that needs to be invoked from python
2 min read
How to Get Current Branch name in GitHub Actions
GitHub Actions rightfully deserves credit for the way the workflow automation in the GitHub is being redefined. No matter it has been about continuous integration, deployment or the automation of any custom operation, GitHub Actions provide a framework that is advanced enough to make these tasks eas
4 min read
How do I use Docker with GitHub Actions?
Docker packages all the components required for software into containers, much like a recipe box. By automating chores in your development workflow, GitHub Actions serves as your personal chef. When combined, they optimize software development by effectively packaging applications and smoothly autom
5 min read