Scripting methods

There are three general ways to run a script file in Bash: sourcing it, providing it to bash as input, and as a standalone script. For the following examples, we'll use the hello.bash script, with the following contents:

printf 'Hello, %s!\n' "$USER"

Sourcing the script means to use the Bash source command to read all of the commands in a script into the current shell session and run them there. A source command to read in a script such as hello.bash might look like this:

bash$ source hello.bash
Hello, bashuser!

This method behaves rather like a function, in that it runs the commands as if you had entered them from your own shell. Variable settings, directory changes, shell options, and other changes will apply to the current ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.