DEV Community

Discussion on: 101 Bash Commands and Tips for Beginners to Experts

Collapse
 
jikuja profile image
Janne Kujanpää

Cool article but there are few things I need to disagree.

Environment variable creation

Using format foo=bar stores environment variable only current command. To keep environment variable loaded in your running bash process you need to export it with export foo=bar. This behaviour can be checked with commands env, printenv or by echoing environment variable.

source vs sh

Results of source and sh commands might look identical but they are not. Source command executes script in running bash and all exported envirenment variables are part of the running bash shell. sh invokes a new shell and exported variables are not usable in the calling shell. Also working directory of calling bash shell might be changed when using source command.

Please note that sh usually is linked to posh or some other lightweight shell which does not have all bash features.

Collapse
 
jikuja profile image
Janne Kujanpää

More about source

In some systems source and . are interchangeable command. (POSIX compatibility is swamp)

Anyway source command is good if you need (temporary) setup your environment: e.g. load environment variables, aliases and even request passwords from user with read -s <variable_name> or to load functions from external libraries.

Usually source is used in scripts(1) but I'm sure that users can find use cases when to use source to setup environment for running shell.