User defined and Built-in variables in shell scripting

In the Unix shell, variables are named values that can be used to store data and manipulate it within scripts or commands. Variables can be assigned using the = operator, and their values can be accessed using the $ notation followed by the name of the variable.

Here is an example of how to create and use a variable in a shell script:

#!/bin/bash
# Declare a variable and assign it a value
MYVAR="Hello, world!"
# Print the value of the variable
echo $MYVAR

This script will print the following output:

Hello, world!

You can also use variables in arithmetic expressions by enclosing them in $(( )) notation. For example:

#!/bin/bash

# Declare variables and assign them values
A=2
B=3# Perform arithmetic operations using the variables
echo "A + B = $((A + B))"
echo "A - B = $((A - B))"
echo "A * B = $((A * B))"
echo "A / B = $((A / B))"

This script will print the following output:

A + B = 5
A - B = -1
A * B = 6
A / B = 0

In the Unix shell, there are several types of variables that you can use:

Scalar variables: These are variables that can hold a single value. They are defined using the declare command followed by the -i flag and the name of the variable, like this:
declare -i A=5

Array variables: These are variables that can hold multiple values. They are defined using the declare command followed by the -a flag and the name of the array, like this:
declare -a ARR=("apple" "banana" "orange")

String variables: These are variables that can hold a string of characters. They are defined using the declare command followed by the -s flag and the name of the variable, like this:
declare -s NAME="John"

Read-only variables: These are variables that cannot be modified or unset. They are defined using the declare command followed by the -r flag and the name of the variable, like this:

declare -r PI=3.14

 

Floating Point variables

In the Unix shell, you can use floating-point values in variables by declaring the variables as integers using the declare command and the -i flag. For example:

# Declare a variable as an integer
declare -i PI=3.14
# Print the value of the variable
echo "The value of PI is: $PI"

This script will print the following output:

The value of PI is: 3

Note that when you declare a variable as an integer, any fractional part of the value will be truncated. If you want to retain the fractional part of the value, you can use the printf command to print the value with a specified number of decimal places. For example:

# Declare a variable as an integer
declare -i PI=3.14
# Print the value of the variable with two decimal places
printf "The value of PI is: %.2fn" $PI

This script will print the following output:

The value of PI is: 3.14

To print a floating-point value in the Unix shell, you can use the echo command or the printf command.

To print a floating-point value using the echo command, you can use the -e flag to enable interpretation of backslash escapes, and then use the %f escape sequence to specify the format of the floating-point value. For example:

$ echo -e "The value of PI is: %f" 3.14

This will print the following output:

The value of PI is: 3.140000

To print a floating-point value with a specific number of decimal places, you can use the printf command and the %.nf format specifier, where n is the number of decimal places. For example:

$ printf "The value of PI is: %.2fn" 3.14

This will print the following output:

The value of PI is: 3.14

test command and conditional statements

The test command in the Unix shell is a utility that allows you to evaluate a condition and return a exit status of 0 (true) or 1 (false). It is commonly used in conjunction with the if and [ commands to control the flow of a script or command.

Here are some examples of how to use the test command:

# Check if a file exists
test -e /path/to/file
# Check if a file is a regular file
test -f /path/to/file
# Check if a file is a directory
test -d /path/to/directory
# Check if a string is empty
test -z "$STRING"
# Check if a string is not empty
test -n "$STRING"
# Check if two strings are equal
test "$STRING1" = "$STRING2"
# Check if an integer is greater than another integer
test "$INT1" -gt "$INT2"

You can also use the [ command as an alternative to the test command. The [ command is actually an alias for the test command, and it can be used in the same way. For example:

# Check if a file exists
[ -e /path/to/file ]
# Check if a file is a regular file
[ -f /path/to/file ]
# Check if a file is a directory
[ -d /path/to/directory ]
# Check if a string is empty
[ -z "$STRING" ]
# Check if a string is not empty
[ -n "$STRING" ]
# Check if two strings are equal
[ "$STRING1" = "$STRING2" ]
# Check if an integer is greater than another integer
[ "$INT1" -gt "$INT2" ]

Built-in Variables in shell CLI

In shell scripting, built-in variables are predefined variables that are available to the shell and can be used in scripts without having to be set or exported first.

$0: The name of the script being executed.

# Print the name of the script
$ echo "Script name: $0"

$1, $2, $3, etc.: The first, second, third, etc. arguments passed to the script.

# Print the first argument passed to the script
echo "First argument: $1"
# Print the second argument passed to the script
echo "Second argument: $2"

$#: The number of arguments passed to the script.

# Print the number of arguments passed to the script
echo "Number of arguments: $#"

$*: All the arguments passed to the script, as a single string.

# Print all the arguments passed to the script as a single string
echo "All arguments: $*"

$@: All the arguments passed to the script, as separate strings.

# Print all the arguments passed to the script as separate strings
echo "All arguments: $@"

$?: The exit status of the last command executed.

$$: The process ID of the current shell.
# Print the process ID of the current shell
echo "Process ID: $$"

$!: The process ID of the last command run in the background.

# Run a command in the background some_command &
# Print the process ID of the last command run in the background
echo "Background process ID: $!"

 


Summary:
In the Unix shell, variables are named values that can be used to store data and manipulate it within scripts or commands. There are several types of variables that you can use, including scalar variables, which can hold a single value; array variables, which can hold multiple values; string variables, which can hold a string of characters; and read-only variables, which cannot be modified or unset. You can also use floating-point values in variables by declaring the variables as integers using the declare command. The test command is a utility that allows you to evaluate a condition and return a exit status of 0 (true) or 1 (false), and it is commonly used in conjunction with the if and [ commands to control the flow of a script or command. The “case” statement is another way to control the flow of a script or command, allowing you to match a value against a list of patterns and execute a block of code when a pattern is matched.

Questions for review:

  1. How do you assign and access a variable in the Unix shell?
  2. How can you perform arithmetic operations using variables in the Unix shell?
  3. What are scalar variables and how are they defined in the Unix shell?
  4. What are array variables and how are they defined in the Unix shell?
  5. What are string variables and how are they defined in the Unix shell?
  6. What are read-only variables and how are they defined in the Unix shell?
  7. How do you declare a variable as a floating-point value in the Unix shell?
  8. How do you print a floating-point value in the Unix shell?
  9. What is the test command and what is it used for in the Unix shell?
  10. How can you use the test command or [ ] in conjunction with the if to control the flow of a script in the Unix shell?

License

Developers ultimate guide: Linux Bash scripting Copyright © 2022 by Matin Maleki. All Rights Reserved.

Share This Book