3
#!/usr/bin/env bash

scriptdir="$HOME"
touch $scriptdir/foo.txt
. "$scriptdir/foo.txt" ||
{ echo "Missing '$scriptdir/foo.txt'. Exiting." ;
exit 1 ;}
echo "$scriptdir/foo.txt is present"
echo
rm "$scriptdir/foo.txt"
. "$scriptdir/foo.txt" ||
{ echo "Missing '$scriptdir/foo.txt'. Exiting." ;
exit 1 ;}

I don't understand the use of . in . "$scriptdir/foo.txt" ||

It seems to be functioning similar to if [ -f "$scriptdir/foo.txt ] , yes?

Such that

scriptdir="$HOME"
touch $scriptdir/foo.txt
 if [ -f "$scriptdir/foo.txt" ] ; then
   echo
 else
 { echo "Missing '$scriptdir/foo.txt'. Exiting." ;
exit 1 ;}
 fi

yields a similar result.

Can someone elaborate on the use of . here?

If I write a script in foo.txt then that script will presumably run because . causes a file to execute rather than just looking to see if it is there? And, as long as $scriptdir/foo.txt is present and executable, then the right half of || will never run because the left half is returning an exit status of zero?

marked as duplicate by jasonwryan, Michael Homer, countermode, don_crissti, thrig Mar 13 '17 at 14:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6

. is a bash built-in, same as source, what it does is that it reads and executes commands from FILENAME in the current shell. Type help . or help source in your terminal to check out it's full documentation.
Full documentation:

.: . filename [arguments]
    Execute commands from a file in the current shell.

    Read and execute commands from FILENAME in the current shell.  The
    entries in $PATH are used to find the directory containing FILENAME.
    If any ARGUMENTS are supplied, they become the positional parameters
    when FILENAME is executed.

    Exit Status:
    Returns the status of the last command executed in FILENAME; fails if
    FILENAME cannot be read.
  • Ok. So, if I write a script in foo.txt then that script will presumably run because . causes a file to execute rather than just looking to see if it is there? And as long as $scriptdir/foo.txt is present and executable then the right half of || will never run because the left half is returning an exit status of zero? @David Dai – Bleakley Mar 13 '17 at 5:30
  • yes, if the file contains commands, these commands will execute. as stated in the document, . returns the status of the LAST command executed in the file, if the last command of the file returns a non-zero the command after || will run. @Bleakley – David Dai Mar 13 '17 at 5:38
  • Hmmm. So I can enter a script that ends with false into $scriptdir/foo.txt causing it to exit nonzero and therefore || will evaluate the right side. Interesting... Thanks! @Dave Dai – Bleakley Mar 13 '17 at 5:52
  • Thanks @JuliePelletier. It is acting the way I want but it seems the echo message on the right side of || is technically incorrect if the foo.txt exits nonzero. – Bleakley Mar 13 '17 at 5:59
  • @JuliePelletier && will not force both to run, instead the latter command will run if and only if the command in front of && returns 0 (true) – Johan Myréen Mar 13 '17 at 7:05

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