0

I'm having difficulty finding information on what the following command is called or does? This command is meant to be called from .rc files.

What is this syntax called in bash and zsh? I know it's for both, as shell file is meant for both. What does it do when placed inside a file? What does it do in file/function/cli?

And what is the equivalent in fish (Friendly Interactive SHell)? Will the same bash/zsh shell file work with that equivalent command in config.fish?

> . /path/to/sh-file.sh    # what does this do? nothing happens on cli when called.

> cat /path/to/sh-file.sh


[ -d "${_DATA:-$HOME/.kdcd}" ] && { echo ... }

_kd () {
....
}
oguz ismail
  • 1
  • 16
  • 47
  • 69
user14492
  • 2,116
  • 3
  • 25
  • 44
  • [Cross site duplicate](https://askubuntu.com/questions/232932/in-a-bash-script-what-does-a-dot-followed-by-a-space-and-then-a-path-mean) – that other guy Jul 19 '20 at 03:12
  • 1
    `'.'` means `source` the named file in the current one. (`'.'` is actually the original operator and `source` more a less an alias for it) You really should see `[ -s /path/to/sh-file.sh ] && . /path/to/sh-file.sh` to ensure the file is non-empty before sourcing it. – David C. Rankin Jul 19 '20 at 03:37
  • Does this answer your question? [What is the meaning of ". filename" (period space filename) in Bash?](https://stackoverflow.com/questions/17014478/what-is-the-meaning-of-filename-period-space-filename-in-bash) – Benjamin W. Jul 19 '20 at 04:16

1 Answers1

3

This . does not refer to the current working directory as it usually does elsewhere. Rather, it is the short (and, it seems, the original) version of the source command, which executes the file specified in the current shell (even if it is not executable), rather than in a child shell, which is what happens when you run with ./file or bash file. Functions defined, variables assigned, and modifications to the environment made in child shells are unknown to the parent shell, so if you want a file to do something to the current shell, you probably want to source it.

$ type .
. is a shell builtin
$ help .
.: . filename [arguments]
    Execute commands from a file in the current shell.

A common use of the source or . commands is to be able to immediately use aliases or functions defined in a particular file. For example, after making changes to a shell rc file, to use the new configuration immediately, one may run . .bashrc (or . .zshrc, etc).

It is also common for one file to source another as you saw. For example, as a shell's rc file is executed automatically in every interactive shell of that type when it starts, you can incorporate configurations in other files in every interactive shell you run by having the rc file source those files.

Both . and source work in the same way in the fish shell, but, of course, the syntax of the functions etc in the file you source may be incompatible with shells other than the one they were written for.

Zanna
  • 205
  • 5
  • 13