bash variable

Bash: variable from commands

To assign the output of a command into a bash variable:

var=$(command)

Bash: passing environment variables

For environment variables to be visible from within a bash script, you must export them.

Given the bash script:

#!/bin/bash
echo $myvar

The environment variable myvar must be set this way:

$ myvar="test"; # This won't work.
$ export myvar="test"; # This works.

Another solution would be to source another script where those variables are defined.
You may source in a sub-shell in order to limit the scope of the variables:

( . ./all_my_variables.sh )

bash: variable scope

See the use of the keywords local and global, the switch -G for declare.

Late evaluation: expressions are evaluated at the last moment.

What is the scope of local? A code flow segment (between { and }, like C++) or a function?

Syndicate content