CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Environment & Shell / source

source

Run a file's commands in the current shell

Beginneressentialshellenvironment

source (or its POSIX name '.') reads a file and runs its commands inside your current shell instead of a new process. Variables, exports, aliases, functions and cd in that file stick, which is how you reload ~/.bashrc or load a project's .env.

Syntaxsource FILE [ARGS]... | . FILE [ARGS]...

Watch it run

Your setenv.sh sets APP_ENV and changes directory. Compare running it with sourcing it.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
cat setenv.sh

Two lines that change the shell's state: an export and a cd.

Anatomy of a command

$ ;
commandset -a

Turn on allexport: every variable assigned from now on is exported automatically.

How it works

Running ./script.sh makes the shell fork a child, which execs the interpreter named in the shebang; the script's changes die with that process. source is a builtin: bash opens the file, reads it and parses and executes each command in the current process, as if you'd typed them. So assignments, export, cd, alias, function definitions, shell options and even exit or return all affect your shell. If the name has no slash, bash looks it up in PATH (unless shopt sourcepath is off) and then the current directory, unlike executing, which never looks in the current directory. The file doesn't need execute permission or a shebang. Arguments after the file name become $1, $2... during the sourcing. 'return' ends a sourced file early; 'exit' in a sourced file exits your whole shell, closing your SSH session. '.' is the POSIX spelling and works in sh and dash, where 'source' may not exist.

Options you'll actually use

source FILERun FILE in the current shell (bash, zsh)
. FILESame, POSIX spelling (sh, dash, bash, zsh)
source FILE ARGSPass arguments as $1, $2... while it runs
set -a / set +aAuto-export every assignment (load .env files)
returnLeave a sourced file early (exit would close your shell)
source ~/.bashrcReload your shell config without logging out

Common mistakes

./activate (or bash venv/bin/activate) to enter a Python virtualenv

It runs in a child; PATH changes vanish when it exits.

source venv/bin/activate
source .env and then running the app

Plain NAME=value lines create unexported shell variables; the app doesn't see them.

set -a; source .env; set +a
source untrusted-file.sh

Every line runs with your privileges in your shell, including anything malicious.

Read it first; for .env-style data, parse KEY=value lines instead of executing them
Using exit in a file meant to be sourced

exit terminates your interactive shell (and your SSH session).

return 1

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create ~/myenv.sh that exports GREETING=hello and changes to /tmp. Run it, then source it, and compare $GREETING and pwd each time.

  2. 2

    Load ~/app/.env so that a child process can see PORT and LOG_LEVEL.

  3. 3

    Reload your shell config without logging out (the sandbox shell is zsh, so the file is ~/.zshrc or /etc/zsh/zshrc).

Related commands

exportPass variables to the programs you startsetShell options: set -euo pipefail, set -x and friendsenvShow the environment, or run a command with a changed onebash: functionsFunctions: name a block, pass arguments, return a status
PrevioushistoryNextecho
Linux / Environment & Shell / source

source

Run a file's commands in the current shell

Beginneressentialshellenvironment

source (or its POSIX name '.') reads a file and runs its commands inside your current shell instead of a new process. Variables, exports, aliases, functions and cd in that file stick, which is how you reload ~/.bashrc or load a project's .env.

Syntaxsource FILE [ARGS]... | . FILE [ARGS]...

Watch it run

Your setenv.sh sets APP_ENV and changes directory. Compare running it with sourcing it.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
cat setenv.sh

Two lines that change the shell's state: an export and a cd.

Anatomy of a command

$ ;
commandset -a

Turn on allexport: every variable assigned from now on is exported automatically.

How it works

Running ./script.sh makes the shell fork a child, which execs the interpreter named in the shebang; the script's changes die with that process. source is a builtin: bash opens the file, reads it and parses and executes each command in the current process, as if you'd typed them. So assignments, export, cd, alias, function definitions, shell options and even exit or return all affect your shell. If the name has no slash, bash looks it up in PATH (unless shopt sourcepath is off) and then the current directory, unlike executing, which never looks in the current directory. The file doesn't need execute permission or a shebang. Arguments after the file name become $1, $2... during the sourcing. 'return' ends a sourced file early; 'exit' in a sourced file exits your whole shell, closing your SSH session. '.' is the POSIX spelling and works in sh and dash, where 'source' may not exist.

Options you'll actually use

source FILERun FILE in the current shell (bash, zsh)
. FILESame, POSIX spelling (sh, dash, bash, zsh)
source FILE ARGSPass arguments as $1, $2... while it runs
set -a / set +aAuto-export every assignment (load .env files)
returnLeave a sourced file early (exit would close your shell)
source ~/.bashrcReload your shell config without logging out

Common mistakes

./activate (or bash venv/bin/activate) to enter a Python virtualenv

It runs in a child; PATH changes vanish when it exits.

source venv/bin/activate
source .env and then running the app

Plain NAME=value lines create unexported shell variables; the app doesn't see them.

set -a; source .env; set +a
source untrusted-file.sh

Every line runs with your privileges in your shell, including anything malicious.

Read it first; for .env-style data, parse KEY=value lines instead of executing them
Using exit in a file meant to be sourced

exit terminates your interactive shell (and your SSH session).

return 1

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Create ~/myenv.sh that exports GREETING=hello and changes to /tmp. Run it, then source it, and compare $GREETING and pwd each time.

  2. 2

    Load ~/app/.env so that a child process can see PORT and LOG_LEVEL.

  3. 3

    Reload your shell config without logging out (the sandbox shell is zsh, so the file is ~/.zshrc or /etc/zsh/zshrc).

Related commands

exportPass variables to the programs you startsetShell options: set -euo pipefail, set -x and friendsenvShow the environment, or run a command with a changed onebash: functionsFunctions: name a block, pass arguments, return a status
PrevioushistoryNextecho