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