Quotes in Bash Shell Scripting

Quoting

Enclosing the command or text with the quotation is a standard practice on the Linux command prompt mostly when you are assigning the values with space, or with some special character .but how to know whether you want to use ‘single quotes’ or “double quotes”?Let me explain this difference

The common rule is You can prevent the shell from substituting the value of a variable by quoting (single quotes), whereas double quotation mark do not prevent the substitution.

Quotes with text

When you are assigning a  few words of text it really doesn’t a matter whether you use, since they both will work exactly the same. Let me show this with one simple example

To Learn the basics of bash shell scripting click this link 👉🏻  http://www.linuxvasanth.com/bash-shell-scripting/

The below two commands will create a file test file.

#touch  'test file'

#touch  "test file"

Bash Shell Variable Expansion:

When you are working with a variable name in the command line the single quotes and double quotes behave very differently.

#myvar="abc is a single string"

#echo $myvar

Sample Output: abc is a single string

Now when you are using the single quotes it prevents substitute the values from the variable

#echo '$myvar'

Sample Output: $myvar

From the above output, it displays only the variable name when you are using with single quotes and double quotation mark do not prevent the substitution.

Note: Single quotes will treat every character Literally i.e Strictly follows the exact word, Double quotes will allow you to do the substitution i.e includes variables within the setting of the value.

Note: Double quotation marks are useful when you assign a value to variables, To assign a value that contains spaces or tabs to a variable, use double quotation marks around the value.

If you found this article useful, Kindly Subscribe here 👉  Click this link to Subscribe

Never miss an article Do like my official  FB page 👉 Learn Linux in an easier way

What is Bash Shell Scripting?

Introduction

As we all know Shells are interactive i.e it accepts the inputs from the keyboard given by a user and  execute them ,Suppose if you have a routine task and that task is having some 20 to 30 Unix commands then it would be difficult for the administrator to execute each and every command one by one to create a report, to ease the administrator task we put all the Required commands in a text file and tell the user to execute them this is called as a shell script

A script could be written to start a database, shut down a database, start a backup program check whether a specific service is up and if not it could start it, so many other tasks can be done using the shell scripts.

NOTE: Writing shell scripts halt automates the administration tasks saves the admin time etc..

Advantages of having Shell Scripting:

1.Ease of use: If you have to run a sequence of commands in your daily task then it is easier to have a shell script file to do them. All you have to do is just execute the file to run the several commands.

2.Easier to Remember: If you are using man arguments and options it is quite easy to miss command options or arguments if you are typing it every time into the command line.

Procedure to create a Shell script file:

1. Create a file with the required commands or codes as per the requirements.

2. The command or code to be run by some interpreter so Add the interpreter in to the script file(here we use “bash” as the interpreter).

3. Add the commands or codes in to the script file

4. Add the execute permission to the script file

5.Run the script file

These all are the steps you need to follow to create a proper shell script file.

Ex:1 Create a basic Monitoring Shell script file

In this example, I will create a script file which is to monitor the server health performance and resources usages.

Step:1

#vi   /script.sh

#!/bin/bash

free - m

who

last |grep reboot

who -r

netstat  -l

netstat -s

cat  /var/log/messages   |wc  -l

:wq!

Step:2 Add the execute permission to the user(here as a root I will execute the script file)

#chmod   u+x   /script.sh

#ls  -l   /script.sh

Step:3 Run the script file

Syntax:  #./scriptfilename

#./script.sh

Note: For better view Output has Truncated🖕🏻

As you can see from the above output the interpreter will read and execute each and every command inside the file and prints the output on the screen.

Note: Here  “.” indicates run the script from your current path, if the script is not in your current path then you will get the message No such file or directory.

I hope you have enjoyed this article…

If you found this article useful, Kindly Subscribe here 👉  Click this link to Subscribe

Never miss an article Do like my official  FB page 👉 Learn Linux in an easier way