Creating Shell Alias in Red-hat Linux

What is Bash Shell Alias?

A Shell Alias is a shortcut to reference a command.It can be used to avoid typing long command.So basically with this alias you can create a short name or can add group of commands including their options and arguments and many more.

Alias is an easier way to define your own commands or you can also create a shortcut name for the commands

Creating Shell Alias:

The syntax to create a new alias

#alias <alias name>=<values>
Ex:1 Creating alias name for the “date” command

In this example i wish to change the name of the command “date” to “life” ,So after assigned i could use the alias name “life” to  display the output instead of calling the “date” command

#alias life=date

The “date” command gives you the above output

New alias name is created successfully,now call the alias name“life “ to display the date command output

Note: Some of you might think the "date" command will not function after creating a new alias name,but in practical Remember both "date" and "life" will run.
Ex:2 Setting default options on commands to avoid having to type them each time a command is run.

In this example i will set the -v and -f option with the “rm” command.

When you run the “rm” command without “-f”(forcefully) option ,the rm command works with the interactive mode i.e  it will ask the confirmation from the end user.Here am going to use -v and -f option ,so that every time i run this command by default it uses -v and -f as their default settings,The user no need to manually give the -f and -v option.

without option the “rm” command works with interactive mode as shown below,

#rm  /mydoc

As you can see from the above output,the “rm” command uses the interactive method before it run

Let me apply the options with the rm command as shown below,

#alias  rm="rm  -vf"

As shown above,whenever i run the “rm” command it uses the value “rm  -vf” as their default mode.

#rm  /data

As you can see from the above output,this time it will not ask any confirmation from the end user, i.e  it uses -f option to ignore the interactive mode and coz of the -v verbose option it prints the message on the screen “removed  /data”

Like wise you can create alias name for commands and also you can set the desired options ,arguments with the command.

NOTE:In this setting an alias in this way only works for the life of a shell session,when the shell is closed the alias will be lost,i.e it uses the permanent setting. In order to make the alias values permanent you will need to update the details inside the .bashrc file.
Ex:3 To Make the alias values Permanent

Inside the /root directory you will have a file with the name .bashrc(.bashrc is a hidden file),so you should use the -a option with the ls command to get the hidden files from your path.

#cd  /root
#ls  -a

#vi  .bashrc

alias rm="rm  -vf"

That’s it ,i have made the alias value as the  permanent  one.

Ex:4 To Unalias the alias name
Syntax:

#unalias  <alias name>

In this example let me remove the alias name “life”(which is created for the date command previously) in our example.

#unalias   life
#life

Once you have removed the assigned alias name,after  that the name is no longer  available,but the “date” command works as usual without any issues as shown in the above output.

Ex:5 To have the “ls” alias always display the contents of the /etc directory,it can be rewritten as shown below,
#alias l="ls -l  /etc"

So whenever i call “l” from the shell prompt it  will display the details of the /etc/ directory

#l

 

Ask a question and VASANTH will respond to you.We are here to help you in any way we can.

How to Create a Bash Shell Script file in Linux? Tutorial video Create a Bash shell script file
You can reach me  at  vasanth@linuxvasanth.com

I hope you have enjoyed this tutorial if so Kindly subscribe and share it with your friends.
                                          🙏🙏 Thank you 🙏🙏
#############################################################
For More Videos Subscribe My Youtube Channel  Linux Vasanth
If you found this article useful, Kindly Subscribe here 👉  Click this link to Subscribe

################################################################################

 

 

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