Sunday, August 2, 2015

How to check if a file exists in Bash

In Unix, Mac, or Windows (Cygwin) you can check if a file exists when you load up the terminal window.  The script is as follows:

if [ -f ~/somefilename ]; then
   source ~/somefilename
fi

In this example the script checks to see if a file named "somefilename" exists in my home/root directory.  If it "somefilename" exists then it sources/executes the content of that file as a script.

When is this useful?  I'll show you in the following post: Turn on Git Autocompletion


Links
Bash Scripting: How to
Bash Beginners Guide 
Advanced Bash Scripting

No comments: