Linux: Bash/Shell Scripting and Execution!

While we work in the terminal for hours, scripting can be vital. They can make our work easier and save the day for us. Scripting provides us the ability to automate various activities, hence, running scripts is very important. But how do you actually write a script and run it? Well, that’s what I’ll be teaching you guys here.

What is to be done?

  1. Firstly, we’re going to create a directory (folder) called “scripts” in “/” that is gonna be home to all the scripts we write.
  2. Then, we’re going to assign the path to that directory (scripts) in PATH variable, either permanently or temporarily.
  3. Now, we’re gonna write our own script in a file called “script1” inside the “scripts” directory.
  4. After that, we’re gonna make that file executable.
  5. And finally, we’ll be running that script.

Complete walk-through:

/]# cd / #moves you to /

/]# mkdir scripts #makes a directory called scripts within / /]# PATH=$PATH:/scripts/ #adds the path to the scripts directory where the scripts you write will be stored in the path variable

Note that this is only a local variable, in order to make the change permanent, you should edit the ‘bashrc’ file.
Edit the /etc/bashrc file to make this variable system wide, or edit the ~/.bashrc file to make this a user specific change.

/]# vi /scripts/script1/ #makes a file called script1 in the scripts directory.

Now, you should write your script there and save and exit.
Before we can actually run the script, we need to change the permissions and make it executable.

/]# chmod a+x /scripts/script1 #makes the file executable

Here, to run the script, just type the name of the file where the script is saved!

/]# script1

That’s it! You’re done!
Now, you can make and run your own script, congratulations!

If you guys have any problem running the script, make sure to drop a comment below.

Published by Abhinav Gyawali

Hello there! I'm a teenager, who spends most of his day thinking and analyzing things. I'm full of ideas and creativity! Hence, I realized a blog would be perfect for me to share my ideas!

Leave a comment

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

%d bloggers like this: