DEV Community

Twinkle lahariya
Twinkle lahariya

Posted on • Updated on

Variables and Tests in shell scripting

In the earlier post, we learned the basics of shell scripting and wrote our first shell script.

In this post, let's start with variables in a shell script

What are variables?

Variables are the storage locations that have a name.
Consider variables as a name-value pair.
VARIABLE_NAME="Value"

Variables' names are case sensitive and the general convention is to have them in all uppercase.

Now, how can I use these variables in my script?

Variables can be used by preceding variable names with $.
$<VARIABLE_NAME>
Let's look at an example:

#!/bin/bash
SHELL_NAME="bash"
echo "I am using $SHELL_NAME scripting!"
Enter fullscreen mode Exit fullscreen mode

Output:

>./VariablesDemo.sh
I am using bash scripting!
Enter fullscreen mode Exit fullscreen mode

We can also enclose the name in curly braces
${<VARIABLE_NAME>}
In that case, the above example will look like this:

#!/bin/bash
SHELL_NAME="bash"
echo "I am using ${SHELL_NAME} scripting!"
Enter fullscreen mode Exit fullscreen mode

Output:

>./VariablesDemo.sh
I am using bash scripting!
Enter fullscreen mode Exit fullscreen mode

Is curly braces mandatory?

No, but in some cases, you need to. Let me show you when

In case you want to concatenate the variable with another string you have to use curly braces.

Say you have to write "I am scripting on my machine" and you have a variable "$SCRIPT="script" in that case you will write:
echo "I am ${SCRIPT}ing on my machine"

If you write $SCRIPTing, in that case, the whole string SCRIPTing will become the name and since we do not have a variable with that name, nothing will be put in that place.

Assigning output of a command to a variable

To assign the output of a command to a variable, just use $(command):

#!/bin/bash

SHELL_NAME="bash"
USER_NAME=$(whoami)
echo "$USER_NAME is using ${SHELL_NAME} scripting!"
Enter fullscreen mode Exit fullscreen mode

Output

> ./VariablesExample.sh
twinklekailashlahariya is using bash scripting!
Enter fullscreen mode Exit fullscreen mode

Variable naming conventions

don'ts
  1. Don't start with a number
  2. Don't use special characters. Only _ is allowed and can be at any location

otherwise, go crazy and name that variable of yours as you want.

Tests in a shell script

Scripts are made to replace the effort to sit and scribble a series of commands. What if we want to perform certain actions based on conditional scenarios? If we are typing in the commands manually, we might check the status of the previous command and write the next command accordingly, right?

In shell scripts, we can achieve the same with tests.
To test a condition, simply place the condition within square brackets.
Syntax = [condition-to-test]

If the test passes, the expression exits with exit status 0, i.e. true, else it returns 1, i.e. false.

Let's look at this with an example:

#!/bin/bash

FILENAME=/etc/myFile.txt
if [ -f "$FILENAME" ]
then echo "File exist"
else echo "File does not exist"
fi
Enter fullscreen mode Exit fullscreen mode

Output:

> ./TestsExample.sh
File does not exist
Enter fullscreen mode Exit fullscreen mode

Let's see what we have done here:
FILENAME=/etc/myFile.txt : Defined a variable with a file path.
if [ -f "#FILENAME" ] : An if condition that checks if the file exists at the given path.
[ -f "#FILENAME" ] : Our test condition where -f is a flag that returns true if file exists and is a regular file.
then echo "File exist" : if the file exists, the control flow goes into this condition and prints "File exist" message.
else echo "File does not exist" : otherwise the control falls into this block and prints "File does not exist" message.

Like -f, we have more such test conditions, some of the popular ones are listed below:

-n STRING the length of STRING is nonzero
-z STRING the length of STRING is zero
STRING1 = STRING2 the strings are equal
STRING1 != STRING2 the strings are not equal
INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2
INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER2
INTEGER1 -gt INTEGER2 INTEGER1 is greater than INTEGER2
INTEGER1 -le INTEGER2 INTEGER1 is less than or equal to INTEGER2
INTEGER1 -lt INTEGER2 INTEGER1 is less than INTEGER2
INTEGER1 -ne INTEGER2 INTEGER1 is not equal to INTEGER2
FILE1 -ef FILE2 FILE1 and FILE2 have the same device and inode numbers
FILE1 -nt FILE2 FILE1 is newer (modification date) than FILE2
FILE1 -ot FILE2 FILE1 is older than FILE2
-b FILE FILE exists and is block special
-c FILE FILE exists and is character special
-d FILE FILE exists and is a directory
-e FILE FILE exists
-g FILE FILE exists and is set-group-ID
-G FILE FILE exists and is owned by the effective group ID
-h FILE FILE exists and is a symbolic link (same as -L)
-k FILE FILE exists and has its sticky bit set
-L FILE FILE exists and is a symbolic link (same as -h)
-O FILE FILE exists and is owned by the effective user ID
-p FILE FILE exists and is a named pipe
-r FILE FILE exists and read permission is granted
-s FILE FILE exists and has a size greater than zero
-S FILE FILE exists and is a socket
-w FILE FILE exists and write permission is granted
-x FILE FILE exists and execute (or search) permission is granted

We will look into more examples of these flags in our next post when we look into conditions and loops.

Until next time... Peace ✌️

Top comments (0)