How to Run Bash Script from Any Directory [Step-by-Step Commands]

Shape Image One

How to Run Bash Script from Any Directory [Step-by-Step Commands]

Do you know why does technology rampant? To make our life easy by automating things. By clicking on a single cockpit button, the plane takes off.

But there are series of operations carried out after clicking the button. This is what the script is all about – running a single command to execute many operations.

In this post, I am not going to tell you how to run a plane or the technology behind it.  I am listing all the commands to write and execute shell scripts from any directories to pilot or to automate series of operations.

The script is a file that contains more than one command. When we run this script file, all the commands in that script executes.

Commands for Writing Shell Script

  • Create a directory where you want to save all your scripts (I named it bin).
mkdir bin
  • Write your first shell script with or without extension .sh.
vim firstScript

And write your Hello world shell script in a file.

#!/bin/bash 
#First shell script by Aniruddha 
echo "Hello, World!"

Save the file.

  • Testing your First shell script file
cd <path/to/file> ./firstScript

Output:

Hello, World!

./ depicts running the script from the current directory.

Now, our executable shell script is ready.

Run Bash Script from Any Directory

Now, how to run the script from anywhere?

  • Add this directory path to the PATH environment variable by the following command.
export PATH=$PATH:<path/to/file>

Note: Before updating the PATH environment variable, save it. You can check the PATH variable value by running the command $PATH.

This path variable will last for the given session.

  • If you want it to make it as a permanent change, you have to update filebashrc.
vim ~/.bashrc
  • Add the following line at the bottom of bashrc file
export PATH=$PATH:<path/to/file>
  • Save the file.

Note: The changes you make in the file bashrc get reflected only when you start the shell next time.

  • If you want it to make it work immediately instead of restarting the shell, run the following commands.
source ~/.bashrc

If you get an error as “Permission denied”, you have to permit executable permission using commandchmod

chmod 777 ~/.bashrc
  • Now, the setup is done and you can execute shell script from any directories.
firstScript

If you get the error as “firstScript: command not found“, you have not set the PATH correctly.

Also, read Bash Scripting vs Python. Which one should you learn?

What are the things you can automate with a shell script?

Let’s see some of the basic usage of bash script with examples.

1. Write a Bash Script to List the Directory Files in Detail

Write a bash script and save the code in a file ls.

#!/bin/bash 
ls -la

Above ls is Unix command to list all the directory files and “la" is an option to print list long format including hidden files. You can find more options for the command “ls”.

Now, whenever you run the commandls, it executes command ls -la and gives you a detail description about directories.

2. Write bash script to log in to a Remote Server without typing Username and Password

If you work on the remote server, you have to log in it first with command ssh. And you will be asked for the login credential. It is very tedious to type username and password every time you want to work on the remote server.

Instead of this, write a shell script for command ssh  and mention the login username and password. Save it in file .sh. Now whenever you run this file, you will be login to a remote server without providing a username and password explicitly.

3. Write bash script for Automation Testing

If you are a tester or testing the application, you can run the test case by writing it in a shell script. You don’t need to run each test case manually.

[disclaimer] I am not a testing guy, so I don’t much about testing. But, there are so many testers in our team uses automation script for testing.

Wrapping up…

There are so many things you can automate on your Linux system by shell scripting. These are some basic applications, you can start with.

This is all about how to run bash script from any directory. Any challenges while executing a shell script from any directories? Feel free to write in a comment. And also, let me know, what are the things you have automated with the shell script?

Leave a Reply

Your email address will not be published. Required fields are marked *