How to Execute Shell Script from Any Directories [Step by Step Commands]
Do you know why does technology rampant? To make our life easy by automating the things. By clicking on 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 single command to execute many operations.
In this post, I am not gonna to tell you how to run a plane or technology behind it. I am listing all the commands to write and execute shell script from any directories to pilot or to automate series of operation.
The script is a file which contents more than one command. When we run this script file, all the commands in that script executes.
- Writing Hello World Bash Script
- How to execute shell script from any directories?
- Simple use cases for Bash script
Commands for writing Shell Script:
- Create a directory where you want to save all your scripts (I named it as bin).
- Write your first shell script with or without extension
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
./ depicts running the script from the current directory.
Now, our executable shell script is ready.
Execute Shell Script from any Directories
Now, how to run the script from anywhere?
- Add this directory path to PATH environment variable by the following command.
Note: Before updating PATH environment variable, save it. You can check the PATH varibale value by running command
This path variable will last for the given session.
- If you want it to make it as a permanent change, you have to update file
- Add following line at the bottom of
- 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 shell, run following commands.
If you get an error as “Permission denied”, you have to permit executable permission using command
chmod 777 ~/.bashrc
- Now, setup is done and you can execute shell script from any directories.
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?
#1 Listing 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 command
Now, whenever you run the command
ls, it executes command
ls -la and gives you a detail description about directories.
#2 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 everytime you want to work on the remote server.
Instead of this, write a shell script for command
ssh and mention 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 username and password explicitly.
#3 Automation Testing:
If you are tester or testing of the application, you can run the test case by writing it in a shell script. You don’t need to run each test cases manually.
[disclaimer] I am not testing guy, so I don’t much about testing. But, there are so many testers in our team uses automation script for testing.
There are so many things you can automate on your Linux system by shell scripting. These are some basic applications, you can start with.
Any challenge 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?