What do arguments to the "source" command do?

Viewed 237

I've encountered the following command:

source foo -c configs/foo.config

What is -c flag is actually may do here? foo is bash script, foo.config also looks like one.

1 Answers

The source command is a bash-specific alias for the POSIX-standardized command .. The specific usage you're asking about is mentioned by POSIX as an allowable extension:

The KornShell version of dot takes optional arguments that are set to the positional parameters. This is a valid extension that allows a dot script to behave identically to a function.

Thus, when code read from foo is invoked, its $1 will be -c and its $2 will be configs/foo.config.


This is also explicitly documented if you run help source in bash:

source: source 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.

Note the text above: If any ARGUMENTS are supplied, they become the positional parameters when FILENAME is executed.

Related