Various sauces

Going through some old Bash files, I noticed this kind of thing

source filename.sh

Here’s a good answer on Stack Overflow explaining why the source command is used:

Running the command source on a script executes the script within the context of the current process. This means that environment variables set by the script remain available after it’s finished running. This is in contrast to running a script normally, in which case environment variables set within the newly-spawned process will be lost once the script exits. You can source any runnable shell script. The end effect will be the same as if you had typed the commands in the script into your terminal.

For example, if the script changes directories, when it finishes running, your current working directory will have changed.

To see this, create a file

test.sh

with contents

#!/bin/sh
cd movies

and a directory movies.

mkdir movies

Change the file’s permissions

chmod u+x test.sh

Run the file normally

./test.sh
pwd

which shows the same directory.

Now try running the file with source

source test.sh
pwd

which shows you are in movies.