Linux Shell Scripting part-2: Message Displaying, User Variables and Environment Variables

In our last post on Shell Scripting, Starting with Linux Shell Scripting you have seen how to start with shell scripts.

Shell scripts may or may not display message depending upon the job it was intended to perform. The output of a Shell script (not message) can be input of another shell script. In most general case a shell script produce their own output. Here is an example of shell script that produce their own output on the console.

Message displaying in shell script

Shell scripts may or may not display message depending upon the job it was intended to perform. The output of a Shell script (not message) can be input of another shell script. In most general case a shell script produce their own output. Here is an example of shell script that produce their own output on the console.

#!/bin/bash
date; time
Sample Output

shell script that produce their own output

However if you want to add custom test messages to your shell script, you may use echo. It helps making your script initeractive and/or let the end user know what is going on. Here is an example of shell script that contains custom text messages.

#!/bin/bash
echo "The Current date is 
date

echo "The Current logged in Users are "
time
Sample Output

byzanz_2016-05-24_150233

echo is one of those commands you are going to use very frequently in shell scripting language. Remember to delineate text strings you need to use either single quote or double quote within text. If you use one type of quote in text, use other to delineate the string. You can use echo command anywhere in your shell script.
An example of echo command having both types of quotes are

#!/bin/bash
echo "Let's figure out who's logged in"
time
Sample Output

byzanz_2016-05-24_193712

Till now we have been using ‘echo’ and actual command on two different lines. How about using both in a single line?

#!/bin/bash
echo -n "Hi there, Today's date is "
date
Sample Output

byzanz_2016-05-24_194439

echo can be very useful while working with variables to display values of variables to the end user.

Variables in Shell Scripting Language

Variable allows you to store the output of other commands(not necessarily, but usually), temporarily to be called later in the script. A simple example of storing variables like integer, Float and String in a shell script is

#!/bin/bash
integer=2
float=2.68
string="Sk46tZ"
echo -e "The Value of Integer is $integer"
echo -e "The Value of Float is $float"
echo -e "The Value of String is $string"
Sample Output

byzanz_2016-05-25_131533

Since the shell script allows to define such variable to users, it is called as user variables. Variables makes your script work like a computer program. A few things to remember about user defined variables

  1. User defined Variables can be any text string of 20 letters and digits.
  2. You can use Underscore in the name of the variables.
  3. Variables are Case-sensitive.
  4. You can use/call your variables any number of times in your script.
  5. The value of a variable within the shell is true only while the life of a Shell. The values are deleted automatically once the script execution is completed.
  6. Last but not the least shell automatically determines the data-type of the variable and you need not bother about it.

Environment Variables

You must have heard about Environment variables. Environment Variables may be considered as system defined variables. The Shell tracks specific system information using these variables. To get a list of all available and enabled environment Variables, you may like to use ‘set’ command.

$ set
Sample Output

ezgif.com-optimize

You may use these Environment variables in your script, as you have used user defined variables. The only difference is you don’t need to define Environment variables, as they are already defined by the shell. An example of shell script calling Environemnt Variables (using variables from the output of set command)

#!/bin/bash
echo -e "My Default Bash Shell is $BASH"
echo -e "My Home Directory is $HOME"
echo -e "And the Hostname is $HOSTNAME"
echo -e "OS type is $OSTYPE"
echo -e "My shell is $SHELL"
echo -e "My User Name is $USERNAME"
Sample Output

byzanz_2016-05-25_140326
Note that you can write a script that usages variables of both kinds.

That’s all for now. In the next part of this article we will look into backtick, Redirection of Input/Output, Pipes and a lot more. Till then stay healthy, tuned and connected to Unixmen. Don’t Forget to give your valuable feedbacks in the comments below.

Like and share us and help us get spread.