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

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

 

 

About Author:

Hello readers! Let me introduce my self first. My name is Vasanth Nirmal Singh J S having 9+ years of experience in IT on all flavours of Unix operating systems ,Storage's and many more .. I would like to share my technical experience i have come across - can be help to other people. So in this blog, I'll post my thoughts related to ITIS. I'll share experiences that I've had while working in different environments. You can expect content related to Unix,Solaris,Linux,EMC Storeages,HP-UX and many others. I hope this blog can be useful for you! Your comments will be appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *