I'm new to bash and am trying to write a simple script.

The user executes my script and in the same line specifies the destination. In this destination, which I do not know in advance, I'd like to check if a specific hidden file exists and do something, and if not create the file. The input looks as such:

./scriptName.sh /userPath

I'm sure I'm overlooking some simple command, and I'd really appreciate any help. Thank you


In your example decide if the path to reach the supplied file will be

relative ( some/child/dir/some_file )

or absolute ( /a/full/path/to/some_file )

... NOTICE the relative has no leading / whereas the absolute does

Then to determine whether that file exists just do


set -o errexit  # stop execution on error

given_file=$1 # retrieve input parameter and populate a variable

if [ -f given_file  ]; then
     echo do something here file does exist
    echo no file 

After you have created your file scriptName.sh you need to turn on its execute bit by issuing

chmod +x scriptName.sh 

do a file listing to show permissions before and then after issuing above then you are ready to execute it

scriptName.sh  /a/full/path/to/some_file 
  • The input path will be absolute. But still, how do I know what path is given? – Life of pi Nov 9 '16 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.