Advanced Unix Shell Scripting: Arrays, File and String Test Operators, Special Variables

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated March 7, 2024

Overview of Advanced Unix Shell Scripting:

Unix shells provide a very powerful and extensible framework for scripting.

A number of advanced mechanisms allow for more complex scripting using the inbuilt commands. 

Advanced Unix Shell Scripting

Unix Video #24:

Advanced Shell Scripting in Unix

Unix includes commands for:

  • Testing various conditions associated with specified files.
  • Testing various conditions associated with specified strings.
  • Performing file read/write operations.

Arrays

Arrays are used to store a series of values in an indexed list. Items in an array are stored and retrieved using an index. Note that Arrays are not supported by the original Bourne Shell, but are supported by bash and other newer shells.

File Test Operators

Shell scripts often need to check various properties of files as a part of the control flow. Unix provides a number of options for this purpose.

  • File existence checks:
    • -f file True if the file exists and is an ordinary file.
    • -d file True if the file exists and is a directory.
    • -s file True if the file exists and is not empty.
    • -c file True if the file exists and is a character device file.
    • -b file True if the file exists and is a block devise file.
  • File access checks:
    • -r file True if the file exists and has read permission to it.
    • -w file True if the file exists and has a write permission to it.
    • -x file True if the file exists and has a execute permission to it.

String Test Operators

Unix commands often need to test the various properties of string variables as a part of the control flow.

Unix provides a number of options for this:

  • [ string1=string2 ] True if string1 and string2 are same.
  • [ string1!=string2 ] True if string1 is not equal to string2.
  • [ -n string ] True if the string is not zero.
  • [ -z string ] True if the string is zero.
  • [ string ] True if the string is not empty.

Special Variables

While running scripts, Unix provides a number of predefined variables that can be used to get information from the environment.

Unix also provides a number of special symbols with additional information:

  • $# Total number of positional parameters.
  • $@ Represents all the parameters i.e. $1 to the end.
  • $? Pass or fail status of the last command executed.
  • $$ Process id of the currently running shell.
  • $! Process id of the last run background process.

Hope you enjoyed all the tutorials on this entire Unix tutorial training series. I’m sure that you would have gained immense knowledge on the various concepts in Unix.

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

Leave a Comment