Bash is a varied shell interface with many programming options, and a rich instructional language. It is easy to miss Bash features and dynamics, so this series introduces a number of tips, tricks, examples and gotchas when it comes to using Bash. For the first two article in this series, please see our article Useful Bash command line tips and tricks examples part 2 and Useful Bash command line tips and tricks examples part 3.

In this tutorial series you will learn:

  • Useful Bash command line tips, tricks and methods
  • How to interact with the Bash command line in an advanced manner
  • How to sharpen your Bash skills overall and become a more proficient Bash user
Useful Bash command line tips and tricks examples - Part 3
Useful Bash command line tips and tricks examples - Part 3

Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Linux Distribution-independent
Software Bash command line, Linux based system
Other Any utility which is not included in the Bash shell by default can be installed using sudo apt-get install utility-name (or yum install for RedHat based systems)
Conventions # - requires linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires linux-commands to be executed as a regular non-privileged user

Example 1: Correctly checking for the existence of files and directories

We can check for the presence of a directory rather easily by using the -d (does a directory with the specified name exist) clause in an if statement:

$ MYPATHTOCHECKFOREXISTENCE="${PWD}"
$ echo "${MYPATHTOCHECKFOREXISTENCE}"
/home/roel/iamhappy
$ if [ -d ${MYPATHTOCHECKFOREXISTENCE} ]; then echo "Exists!"; fi
Exists!

SUBSCRIBE TO NEWSLETTER
Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


However, it is equally easy to make a hard-to-debug mistake in some areas of Bash. For example, let’s consider (and see if you can find the bug);

$ MYPATHTOCHECKFOREXISTANCE="/doesnotreallyexist"
$ if [ -d ${MYPATHTOCHECKFOREXISTENCE} ]; then echo "Exists!"; fi
Exists!
$ ls /doesnotreallyexist
ls: cannot access '/doesnotreallyexist': No such file or directory

Why does the if check come to the conclusion that the /doesnotreallyexist directory does exist? Can you see the bug?

The issue here is that there is a typo on in the variable name. EXISTANCE vs EXISTENCE

Perhaps a bit cheeky, but in all seriousness this should also highly an easy to run into gotcha:

$ if [ -d ]; then echo "Exists!"; fi
Exists!

And more significantly;

$ VAR1=''; if [ -d ${VAR1} ]; then echo "Exists!"; fi
Exists!

Thus, if you forgot the initialize the directory name variable that you are subsequently checking, or the variable name is misspelled, then the result will be that the Bash if statement returns that the directory exists! There is no further mention about this interesting exception in the manual (ref man Bash) which only clarifies that -d is True if file exists and is a directory..

So how can we fix this?

Example 2: A better way to check for the existence of files and directories

The fix is easy; we can quote our variable with double quotes ("), which will then make the if not default to the always-true result. As an interesting side note for further thought, and perhaps your knowledgeable comment below to the same effect, one has to wonder why it was implemented like this and what the underlying always-true implementation is.

$ VAR1=''; if [ -d "${VAR1}" ]; then echo "Exists!"; fi
$


A simple and elegant solution. Instead of the if statement now being parsed as if [ -d ] which, as we have seen, always evaluates to true, now it is parsed as (provided that VAR1 is empty at least) if [ -d "" ] which results in false, and thus the then clause is not executed.

Example 3: Ever wanted to extract the contents of a .deb file?

Sometimes something breaks on a system, and we may want to obtain a single file from a .deb package. .deb packages (debian-style installation packages, as also used by Ubuntu and Mint) can be readily found online, but it is not always so self-evident as to how to extract files from it. To do so, we can:

ar x some_deb_file.deb
tar -xf data.tar.xz

ar is a tool to create, modify, and extract from archives, as the manual (man ar) explains. These commands will yield the files inside the .deb package. Each .deb file will have two archive files, namely control.tar.xz and data.tar.xz (a common standard), and - as you can see from the example - it is the data.tar.xz archive which needs to be expanded to see the files inside the .deb package.

Conclusion

In this article, we explored ways to correctly check for the existence of files and directories by using proper quoting, and we exemplified how it easy to make mistakes in this area. Always test your scripts and try a variety of different situations and scenario’s. We also looked into how we can extract contents from a .deb file using the ar and tar commands. As always, enjoy Bash coding and leave us a comment below with your findings!

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in:



Comments and Discussions