Start a New Bourne-again Shell (Bash) Session
If y'all have to do information technology more than in one case, automate it!
You lot volition frequently find yourself repeating a single task on Linux over and over again. It may be a simple backup of a directory or it could be cleaning up temporary files or it tin can even be cloning of a database.
Automating a task is i of the many useful scenarios where you lot can leverage the power of bash scripting.
Permit me show yous how to create a simple bash shell script, how to run a bash script and what are the things yous must know well-nigh beat scripting.
Create and run your first shell script
Let'southward first create a new directory named scripts that will host all our fustigate scripts.
mkdir scripts cd scripts
At present inside this 'scripts directory', create a new file named how-do-you-do.sh using the true cat command:
true cat > hello.sh
Insert the following line in information technology by typing it in the terminal:
repeat 'Hello, World!'
Press Ctrl+D to save the text to the file and come out of the cat control.
You can also utilize a final-based text editor like Vim, Emacs or Nano. If you are using a desktop Linux, you lot may besides use a graphical text editor like Gedit to add the text to this file.
So, basically you lot are using the echo control to print "Howdy World". You can utilise this command in the final direct but in this test, y'all'll run this control through a vanquish script.
Now make the file hello.sh executable by using the chmod command equally follows:
chmod u+ten hello.sh
And finally, run your first shell script past preceding the hello.sh with your desired vanquish "bash":
bash hello.sh
Yous'll encounter Hello, Globe!
printed on the screen. That was probably the easiest Hello World program you take ever written, correct?
Here's a screenshot of all the steps yous saw above:
Convert your beat script into bash script
Confused? Don't be dislocated but however. I'll explain things to you.
Bash which is short for "Bourne-Again shell" is just one type of many available shells in Linux.
A shell is a command line interpreter that accepts and runs commands. If yous take ever run any Linux command before, then you take used the vanquish. When you open a terminal in Linux, yous are already running the default shell of your system.
Bash is often the default shell in about Linux distributions. This is why bash is often synonymous to shell.
The beat out scripts often have almost the same syntaxes, but they also differ sometimes. For example, assortment index starts at 1 in Zsh instead of 0 in bash. A script written for Zsh trounce won't work the same in bash if information technology has arrays.
To avoid unpleasant surprises, you should tell the interpreter that your shell script is written for bash shell. How exercise you do that? Yous use shebang!
The SheBang line at the beginning of crush script
The line "#!/bin/bash" is referred to every bit the shebang line and in some literature, it's referred to every bit the hashbang line and that's because it starts with the two characters hash '#' and bang '!'.
#! /bin/bash echo 'Hello, World!'
When you lot include the line "#!/bin/bash" at the very meridian of your script, the system knows that you desire to use fustigate as an interpreter for your script. Thus, you can run the how-do-you-do.sh script direct at present without preceding it with bash.
Adding your shell script to the PATH (and so that information technology can exist run from whatsoever directory)
You lot may accept noticed that I used ./hello.sh to run the script; you will get an error if you omit the leading ./
[email protected]:~/scripts$ how-do-you-do.sh hullo.sh: command not plant
Bash thought that you were trying to run a control named hello.sh. When you run any control on your terminal; they shell looks for that command in a ready of directories that are stored in the PATH variable.
You lot can use echo to view the contents of that PATH variable:
repeat $PATH /home/user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
The colon graphic symbol (:) separates the path of each of the directories that your shell scans whenever you lot run a control.
Linux commands like echo, cat etc tin can be run from anywhere because their executable files are stored in the bin directories. The bin directories are included in the PATH. When you run a command, your organisation checks the PATH for all the possible places it should expect for to find the executable for that command.
If yous want to run your bash script from anywhere, equally if it were a regular Linux command, add the location of your beat out script to the PATH variable.
First, get the location of your script's directory (assuming you are in the same directory), employ the PWD control:
pwd
Use the export command to add your scripts directory to the PATH variable.
export PATH=$PATH:/home/user/scripts
Notice that I have appended the 'scripts directory' to the very cease to our PATH variable. And then that the custom path is searched later on the standard directories.
The moment of truth is here; run hello.sh:
[email protected]:~/scripts$ howdy.sh Hello, Globe!
It works! This takes us to the cease of this tutorial. I hope you now have some basic idea about shell scripting. You tin download the PDF beneath and practice what you lot learned with some sample scripting challenges. Their solutions are likewise provided in case you need hints.
Since I introduced y'all to PATH variable, stay tuned for the next bash scripting tutorial where I discuss shell variables in particular.
Source: https://linuxhandbook.com/run-shell-script/
0 Response to "Start a New Bourne-again Shell (Bash) Session"
Post a Comment