Dot (command)

Last updated

In a Unix shell, the full stop called the dot command (.) is a command that evaluates commands in a computer file in the current execution context. [1] In the C shell, a similar functionality is provided as the source command, [2] and this name is seen in "extended" POSIX shells as well. [3] [4]

Contents

The dot command is not to be confused with a dot file, which is a dot-prefixed hidden file or hidden directory. Nor is it to be confused with the ./scriptfile notation for running commands, which is simply a relative path pointing to the current directory (notated in Unix as a '.' character, and typically outside of the Path variable).

Arguments

The filename is the dot command's first argument. When this argument does not contain a slash, the shell will search for the file in all directories defined in the PATH environment variable. Unlike normal commands which are also found in PATH, the file to source does not have to be executable. Otherwise the filename is considered as a simple path to the file. [1]

In several "extended" shells including bash, [3] zsh [4] and ksh, [5] one may specify parameters in a second argument. If no parameters are specified, the sourced file will receive the set of positional parameters available in the current context. If parameters are specified, the sourced file will receive only the specified parameters. In any case, parameter $0 will be the $0 of the current context.

Usages

Since the execution of the source file is done in the invoking context, environment [note 1] changed within apply to the current process or the current shell. This is very different from scripts run directly by shebang or as sh foo.sh, which are run in a new, separate process space, with a separate environment.

Therefore, the dot command can be used for splitting a big script into smaller pieces, potentially enabling modular design. Sourcing is also often done by the shell on session startup for user profile files like .bashrc and .profile.

Source

source is a shell-builtin command that evaluates the file following the command, as a list of commands, executed in the current context. [6] Frequently the "current context" is a terminal window into which the user is typing commands during an interactive session.

The source command can be abbreviated as just a dot (.) in Bash and similar POSIX-ish shells. However, this is not acceptable in C shell, where the command first appeared.

Some Bash scripts should be run using the source your-script syntax rather than run as an executable command, e.g., if they contain a change directory (cd) command and the user intends that they be left in that directory after the script is complete, or they contain an export command and the user wants to modify the environment of the current shell. Another usage situation is when a script file does not have the "execute" permission. Passing the script filename to the desired shell will run the script in a subshell, not the current context.

Notes

  1. See Shell Execution Environment in POSIX.1:2013.

Related Research Articles

<span class="mw-page-title-main">Bash (Unix shell)</span> GNU replacement for the Bourne shell

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been used as the default login shell for most Linux distributions. Bash was one of the first programs Linus Torvalds ported to Linux, alongside GCC. A version is also available for Windows 10 and Windows 11 via the Windows Subsystem for Linux. It is also the default user shell in Solaris 11. Bash was also the default shell in versions of Apple macOS from 10.3 to 10.15, which changed the default shell to zsh, although Bash remains available as an alternative shell.

<span class="mw-page-title-main">Shell script</span> Script written for the shell, or command line interpreter, of an operating system

A shell script is a computer program designed to be run by a Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. A script which sets up the environment, runs the program, and does any necessary cleanup or logging, is called a wrapper.

<span class="mw-page-title-main">Unix shell</span> Command-line interpreter for Unix operating system

A Unix shell is a command-line interpreter or shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting language, and is used by the operating system to control the execution of the system using shell scripts.

<span class="mw-page-title-main">Bourne shell</span> Command-line interpreter for operating systems

The Bourne shell (sh) is a shell command-line interpreter for computer operating systems.

<span class="mw-page-title-main">Z shell</span> Unix shell

The Z shell (Zsh) is a Unix shell that can be used as an interactive login shell and as a command interpreter for shell scripting. Zsh is an extended Bourne shell with many improvements, including some features of Bash, ksh, and tcsh.

pwd Directory information command on various operating systems

In Unix-like and some other operating systems, the pwd command writes the full pathname of the current working directory to the standard output.

An environment variable is a user-definable value that can affect the way running processes will behave on a computer. Environment variables are part of the environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.

In computer programming, glob patterns specify sets of filenames with wildcard characters. For example, the Unix Bash shell command mv *.txttextfiles/ moves all files with names ending in .txt from the current directory to the directory textfiles. Here, * is a wildcard standing for "any string of characters except /" and *.txt is a glob pattern. The other common wildcard is the question mark (?), which stands for one character. For example, mv?.txtshorttextfiles/ will move all files named with a single character followed by .txt from the current directory to directory shorttextfiles, while ??.txt would match all files whose name consists of 2 characters followed by .txt.

xargs is a command on Unix and most Unix-like operating systems used to build and execute commands from standard input. It converts input from standard input into arguments to a command.

time (Unix) Command in Unix and Unix-like operating systems

In computing, time is a command in Unix and Unix-like operating systems. It is used to determine the duration of execution of a particular command.

In computing, echo is a command that outputs the strings that are passed to it as arguments. It is a command available in various operating system shells and typically used in shell scripts and batch files to output status text to the screen or a computer file, or as a source part of a pipeline.

Command-line completion is a common feature of command-line interpreters, in which the program automatically fills in partially typed commands.

In Unix-like and some other operating systems, find is a command-line utility that locates files based on some user-specified criteria and either prints the pathname of each matched object or, if another action is requested, performs that action on each matched object.

<span class="mw-page-title-main">Comparison of command shells</span>

A command shell is a command-line interface to interact with and manipulate a computer's operating system.

test is a command-line utility found in Unix, Plan 9, and Unix-like operating systems that evaluates conditional expressions. test was turned into a shell builtin command in 1981 with UNIX System III and at the same time made available under the alternate name [.

In computing, a shebang is the character sequence consisting of the characters number sign and exclamation mark at the beginning of a script. It is also called sharp-exclamation, sha-bang, hashbang, pound-bang, or hash-pling.

A batch file is a script file in DOS, OS/2 and Microsoft Windows. It consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file. A batch file may contain any command the interpreter accepts interactively and use constructs that enable conditional branching and looping within the batch file, such as IF, FOR, and GOTO labels. The term "batch" is from batch processing, meaning "non-interactive execution", though a batch file might not process a batch of multiple data.

PATH is an environment variable on Unix-like operating systems, DOS, OS/2, and Microsoft Windows, specifying a set of directories where executable programs are located. In general, each executing process or user session has its own PATH setting.

The restricted shell is a Unix shell that restricts some of the capabilities available to an interactive user session, or to a shell script, running within it. It is intended to provide an additional layer of security, but is insufficient to allow execution of entirely untrusted software. A restricted mode operation is found in the original Bourne shell and its later counterpart Bash, and in the KornShell. In some cases a restricted shell is used in conjunction with a chroot jail, in a further attempt to limit access to the system as a whole.

<span class="mw-page-title-main">Command-line interface</span> Computer interface that uses text

A command-line interface (CLI) is a means of interacting with a device or computer program with commands from a user or client, and responses from the device or program, in the form of lines of text. Such access was first provided by computer terminals starting in the mid-1960s. This provided an interactive environment not available with punched cards or other input methods.

References

  1. 1 2 "POSIX.1:2013 Shell Command Language § dot" . Retrieved 23 May 2016.
  2. "Csh man page#command" . Retrieved 23 May 2016.
  3. 1 2 "Bash Reference Manual § Bourne Shell Builtins" . Retrieved 23 May 2016.
  4. 1 2 "ZSH Shell Builtin Commands" . Retrieved 23 May 2016.
  5. "ksh man page". Oracle. Retrieved 23 May 2016. + . name [arg ...]
  6. "BASH BUILTIN COMMANDS", the Linux man page for "source" in Bash-3.0. 2004 Apr 20