Table of Contents
This tip is about the how to Make and Implement Bash Scripts on Linux. So read this free guide, How to Make and Implement Bash Scripts on Linux step by step. If you have query related to same article you may contact us.
How to Make and Implement Bash Scripts on Linux – Guide
I am a new Linux system user. How would I run a Bash script on Linux? How would I write and run a Bash script on the Linux system? This page shows different steps to compose and run a Bash script on Linux using the Terminal window or application.
Create your first script
Making a bash script is much simpler than you might think. Create a file called hello-world using the touch command.
ring hello world
Edit the file with the program of your choice. Inside the file, print a string that says “Hello world! ‘ using echo.
echo “Hello world!”
Now, from the command line, run the script using the bash interpreter:
bash hello world
You will see the script run successfully from the output. That’s it, you created your first script!
Executable Scripts
so far you have learned how to run a script from the command line prefixed with the bash interpreter. However, if you want to run the script by name only, it won’t work. Try running the file by simply typing the filename and pressing enter. Note that we are prefixing the file with ./ which means a file in the current directory.
./Hello World
To run a file directly, we will need to change the permissions to allow the script to be executable for the user. chmod is a command that changes permissions on a file and + x will add execution rights to the script.
chmod + x hello-world
To interpret the file as an executable, you will also have to add the shebang (#!) at the top of the script. On Unix-like systems, a text file with a shebang is interpreted as an executable file. You can confirm where the bash interpreter is located with which bash.
which party
We will add #! /bin/bash to start of script.
echo “Hello world!”
Now you can run hello-world directly.
./Hello World
ropes
A single string in Bash does not require double quotes – you can write it directly.
echo Just a regular string
A single or double quote will expect a match. final, so to use one in such a string, you would need to escape the quote.
However, if you want to use single or double quotes in a string with no escape characters, you can do so by enclosing the string in quotes.
echo ‘A string between single quotes’ echo “A string between double quotes”
With the -e flag, bash will interpret strings with backslash escape characters, such as n for newline. This also requires a quoted string.
echo -e “This string has a nnewline”
Strings enclosed in double quotes are also important for use with variables, as we’ll see in the next section.
Variables
A variable is declared without a dollar sign ($), but has one when called. Let’s edit our hello-world example to use a variable for the entity being greeted, which is World.
#! / bin / bash
who = “World”
echo “Hello, $who!”
Strings enclosed in double quotes are required for interpolating variables. Within a single quoted string, the dollar sign would be interpreted literally
echo ‘Hello, $who!’
Another way to look at variables written is enclosed in square brackets along with the dollar sign, which is known as parameter expansion.
echo “Hello, ${who}!”
This syntax is needed for anything more complex you can do with a variable, like getting an item from an array.
Shell execution
If you want to use the output of a shell execution inside a string, you can do so with a dollar sign followed by parentheses. ($()). For example, the whoami command will print your current user. To use it inside a string, wrap whoami in the shell execution syntax.
echo “Hello, $ (whoami)!”
User input
We declared a variable in the last example, but we can also have the user set the value of a variable dynamically. For example, instead of just having the script say Hello World!, we can have it ask for the name of the person calling the script and then display that name. We will do this using the read command.
#! / bin / bash
echo ‘Who are you?’
read who
echo “Hello, $who!”
Comparison
Operators are slightly different in bash than you are used to. To compare numbers, you will use operators in the number comparison column, such as -lt for less than.
To compare strings, you will use operators in the string comparison column, such as
-eq == Equal-ne! = Different-gt> Greater than-ge> = Greater than or equal to lt
You can also use -z to test for emptiness in a string.
Conditions
if statements use the if, then, else, and fi keywords. The condition is enclosed in square brackets.
check id #! / bin / bash
echo ‘How old are you?’
reading age
What if [ $age -gt 20 ]thenecho ‘You can drink.’elseecho’ You are too young to drink.’fi
rotations
Bash uses for, while, and until loops. In this example, I’ll use the for…in loop to get all the files in a directory and list them.
#! / bin / bash
files = / Users / you / dev / *
to file in $ filesdoecho $ (basename $ file) done
Matrices
A bash matrix is defined in parentheses. There are no commas between array items.
beatles = (‘John’ ‘Paul’ ‘George’ ‘Ringo’)
To access an item from an array, you will use square brackets ([]) Arrays are indexed to 0 in bash. It is also necessary to use parameter expansion syntax.
echo ${beatles[3]}
Final note
I hope you like the guide How to Make and Implement Bash Scripts on Linux. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.