Javascript required
Skip to content Skip to sidebar Skip to footer

What Would You Enter at the Commanf Prompt to Start a Bourne Again Shell Session

If y'all accept to do information technology more once, automate it!

You volition often observe yourself repeating a single task on Linux over and again. It may be a simple backup of a directory or it could be cleaning upward temporary files or it tin even be cloning of a database.

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

Let me show you lot how to create a simple bash shell script, how to run a bash script and what are the things you must know nigh beat scripting.

Create and run your first shell script

Let'south first create a new directory named scripts that volition host all our fustigate scripts.

            mkdir scripts cd scripts          

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

            cat > how-do-you-do.sh          

Insert the post-obit line in it by typing information technology in the terminal:

            repeat 'Hello, World!'          

Press Ctrl+D to save the text to the file and come out of the true cat command.

You lot can also utilize a concluding-based text editor like Vim, Emacs or Nano. If you lot are using a desktop Linux, you lot may also use a graphical text editor similar Gedit to add together the text to this file.

And so, basically you are using the repeat command to print "Howdy World". You can use this control in the final directly but in this test, you lot'll run this command through a crush script.

Now make the file hello.sh executable by using the chmod command as follows:

            chmod u+ten hullo.sh          

And finally, run your first shell script by preceding the hullo.sh with your desired shell "bash":

            bash hi.sh          

You'll see Hi, Earth! printed on the screen. That was probably the easiest Hello World program y'all have ever written, correct?

Here's a screenshot of all the steps y'all saw higher up:

Convert your trounce script into fustigate script

Confused? Don't be confused merely notwithstanding. I'll explain things to y'all.

Bash which is curt for "Bourne-Once more crush" is just one type of many available shells in Linux.

A shell is a command line interpreter that accepts and runs commands. If y'all take ever run any Linux command before, then you have used the shell. When you open a terminal in Linux, yous are already running the default beat out of your arrangement.

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

How to Find Which Shell am I Using in Linux [4 Methods]

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

The shell scripts often take almost the same syntaxes, merely they besides differ sometimes. For instance, array index starts at ane in Zsh instead of 0 in bash. A script written for Zsh shell won't work the aforementioned in fustigate if it has arrays.

To avoid unpleasant surprises, you should tell the interpreter that your shell script is written for bash shell. How do you do that? You utilise shebang!

The SheBang line at the starting time of beat out script

The line "#!/bin/bash" is referred to every bit the shebang line and in some literature, it's referred to equally the hashbang line and that'south considering information technology starts with the two characters hash '#' and bang '!'.

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

When you include the line "#!/bin/bash" at the very top of your script, the system knows that you lot want to use bash as an interpreter for your script. Thus, y'all 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 trounce, available shell in the system and how to change the default crush in Linux.

Adding your trounce script to the PATH (so that it can be run from any directory)

Yous may have noticed that I used ./hi.sh to run the script; you volition get an error if y'all omit the leading ./

            [email protected]:~/scripts$ hi.sh hi.sh: command not found          

Bash thought that you were trying to run a control named hello.sh. When you run any command on your final; they shell looks for that command in a set of directories that are stored in the PATH variable.

You can utilise echo to view the contents of that PATH variable:

            echo $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 character (:) separates the path of each of the directories that your shell scans whenever you run a command.

Linux commands like echo, cat etc can be run from anywhere considering their executable files are stored in the bin directories. The bin directories are included in the PATH. When you lot run a command, your system checks the PATH for all the possible places it should await for to find the executable for that command.

If you desire to run your bash script from anywhere, as if information technology were a regular Linux command, add together the location of your shell script to the PATH variable.

First, become the location of your script'south directory (assuming you are in the same directory), use the PWD command:

            pwd          

Use the consign command to add your scripts directory to the PATH variable.

            consign PATH=$PATH:/abode/user/scripts          
Find that I have appended the 'scripts directory' to the very end to our PATH variable. And then that the custom path is searched after the standard directories.

The moment of truth is here; run how-do-you-do.sh:

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

Information technology works! This takes the states to the terminate of this tutorial. I hope yous at present have some basic idea almost shell scripting. You lot tin download the PDF below and practice what you learned with some sample scripting challenges. Their solutions are likewise provided in example y'all need hints.

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

How to Employ Variables in Bash Beat out Scripts

In the second entry of the bash beginner series, learn about using variables in your bash shell scripts.

mackthaton57.blogspot.com

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