Javascript required
Skip to content Skip to sidebar Skip to footer

The Command Bash Script_file Can Be Used to Directly Execute a Shell Script. True or False?

If you have to do information technology more than in one case, automate it!

Y'all will oft detect yourself repeating a unmarried task on Linux over and over once again. It may be a simple backup of a directory or it could be cleaning upwardly temporary files or it tin fifty-fifty exist cloning of a database.

Automating a task is one of the many useful scenarios where yous can leverage the ability of bash scripting.

Permit me evidence you how to create a simple bash shell script, how to run a bash script and what are the things yous must know about beat scripting.

Create and run your first trounce script

Allow'south commencement create a new directory named scripts that will host all our bash scripts.

            mkdir scripts cd scripts          

Now within this 'scripts directory', create a new file named hello.sh using the cat command:

            true cat > hello.sh          

Insert the post-obit line in it past typing it in the terminal:

            echo 'Hello, Earth!'          

Printing Ctrl+D to save the text to the file and come out of the cat control.

Y'all can also use a terminal-based text editor similar Vim, Emacs or Nano. If you are using a desktop Linux, y'all may also use a graphical text editor similar Gedit to add the text to this file.

So, basically you lot are using the echo command to print "Hello World". You lot tin can use this command in the concluding direct but in this test, you'll run this command through a shell script.

Now make the file hello.sh executable past using the chmod command every bit follows:

            chmod u+x hello.sh          

And finally, run your offset shell script by preceding the hello.sh with your desired trounce "bash":

            bash hullo.sh          

You'll see Hello, Globe! printed on the screen. That was probably the easiest Hello World program you have ever written, right?

Hither's a screenshot of all the steps you saw above:

Convert your shell script into bash script

Dislocated? Don't be confused just nonetheless. I'll explain things to you.

Bash which is brusque for "Bourne-Over again shell" is just one type of many bachelor shells in Linux.

A shell is a control line interpreter that accepts and runs commands. If you have e'er run any Linux command earlier, then you lot have used the beat out. When y'all open a terminal in Linux, you are already running the default shell of your organisation.

Bash is oft the default beat in most Linux distributions. This is why bash is often synonymous to crush.

How to Notice Which Beat out am I Using in Linux [4 Methods]

Here are four simple commands to find out which beat out are you using in Linux.

The trounce scripts oftentimes have almost the aforementioned syntaxes, only they besides differ sometimes. For example, array index starts at one in Zsh instead of 0 in bash. A script written for Zsh crush won't work the same in bash if it has arrays.

To avert unpleasant surprises, you should tell the interpreter that your shell script is written for bash shell. How exercise you practise that? Y'all utilize shebang!

The SheBang line at the beginning of shell script

The line "#!/bin/bash" is referred to equally the shebang line and in some literature, it's referred to as the hashbang line and that's considering it starts with the ii characters hash '#' and bang '!'.

            #! /bin/bash  repeat 'Hello, World!'          

When you include the line "#!/bin/bash" at the very top of your script, the arrangement knows that y'all want to employ bash as an interpreter for your script. Thus, you tin run the hello.sh script directly now without preceding information technology with fustigate.

How to Change Shell in Linux

This quick tutorial shows how to check the running beat, available shell in the system and how to alter the default trounce in Linux.

Adding your beat script to the PATH (and then that information technology tin can be run from any directory)

Yous may have noticed that I used ./how-do-you-do.sh to run the script; you will get an fault if you omit the leading ./

            [email protected]:~/scripts$ hello.sh howdy.sh: control not found          

Bash thought that you lot were trying to run a command named hi.sh. When you lot run any command on your last; they shell looks for that command in a set 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 grapheme (:) separates the path of each of the directories that your beat out scans whenever y'all run a control.

Linux commands like echo, cat etc can exist 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 control, your system checks the PATH for all the possible places it should look for to find the executable for that command.

If y'all want to run your bash script from anywhere, as if information technology were a regular Linux control, add together the location of your shell script to the PATH variable.

First, get the location of your script's directory (bold you are in the aforementioned directory), use the PWD command:

            pwd          

Use the consign 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 so that the custom path is searched after the standard directories.

The moment of truth is here; run hi.sh:

            [email protected]:~/scripts$ howdy.sh Hello, World!          

It works! This takes u.s.a. to the end of this tutorial. I hope you at present have some basic idea nearly beat scripting. Y'all can download the PDF below and practice what you learned with some sample scripting challenges. Their solutions are also provided in instance you need hints.

Since I introduced yous to PATH variable, stay tuned for the next bash scripting tutorial where I discuss shell variables in item.

How to Utilize Variables in Fustigate Shell Scripts

In the 2d entry of the bash beginner serial, learn nigh using variables in your fustigate shell scripts.

stylesscigigive.blogspot.com

Source: https://linuxhandbook.com/run-shell-script/