sourceRun a file's commands in the current shell
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.
source FILE [ARGS]... | . FILE [ARGS]...Your setenv.sh sets APP_ENV and changes directory. Compare running it with sourcing it.
set -aTurn on allexport: every variable assigned from now on is exported automatically.
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.
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 runsset -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./activate (or bash venv/bin/activate) to enter a Python virtualenvIt runs in a child; PATH changes vanish when it exits.
source venv/bin/activatesource .env and then running the appPlain NAME=value lines create unexported shell variables; the app doesn't see them.
set -a; source .env; set +asource untrusted-file.shEvery 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 themUsing exit in a file meant to be sourcedexit terminates your interactive shell (and your SSH session).
return 1Try these on a Linux machine of your own. Each task has a hint and a solution.
Create ~/myenv.sh that exports GREETING=hello and changes to /tmp. Run it, then source it, and compare $GREETING and pwd each time.
Load ~/app/.env so that a child process can see PORT and LOG_LEVEL.
Reload your shell config without logging out (the sandbox shell is zsh, so the file is ~/.zshrc or /etc/zsh/zshrc).
sourceRun a file's commands in the current shell
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.
source FILE [ARGS]... | . FILE [ARGS]...Your setenv.sh sets APP_ENV and changes directory. Compare running it with sourcing it.
set -aTurn on allexport: every variable assigned from now on is exported automatically.
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.
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 runsset -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./activate (or bash venv/bin/activate) to enter a Python virtualenvIt runs in a child; PATH changes vanish when it exits.
source venv/bin/activatesource .env and then running the appPlain NAME=value lines create unexported shell variables; the app doesn't see them.
set -a; source .env; set +asource untrusted-file.shEvery 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 themUsing exit in a file meant to be sourcedexit terminates your interactive shell (and your SSH session).
return 1Try these on a Linux machine of your own. Each task has a hint and a solution.
Create ~/myenv.sh that exports GREETING=hello and changes to /tmp. Run it, then source it, and compare $GREETING and pwd each time.
Load ~/app/.env so that a child process can see PORT and LOG_LEVEL.
Reload your shell config without logging out (the sandbox shell is zsh, so the file is ~/.zshrc or /etc/zsh/zshrc).