Where does the variable assignment go in Bash?

Where does the variable assignment go in Bash?

export exports the variable assignment to child processes of the shell in which the export command was ran. Your command-line environment is the parent of the script’s shell, so it does not see the variable assignment.

What happens to the read variable in Bash?

When the read command sets the variable to the input, that variable is set only in the subshell, not the parent shell. When the subshell exits, the value of the variable is lost. Many pipelines that end with read variable can be converted into command substitutions, which will capture the output of a specified command.

How to assign a variable in a shell?

As always, an assignment in the shell does not accept spaces (see: Spaces in variable assignments in shell scripts ): something like var=foo bar runs the command bar with var set to foo. Or in your case, the command is +, which is just fine, since + has no special meaning to the shell (not in this context anyway).

Why is the export statement not working in Bash?

The export statement works down hierachy (current shell and all it’s subshells) not up as in your example. That causes it to run in your current shell but will not pass variables up the hierarchy either. I often want to set an environment variable without hassle.

Is the assignment to variable [ something ] has no effect?

The code you have is perfectly valid. In effect, you created a constructor that does not assign any variable : and below you created an initializer code block that is called by every constructor : This block effectively assigns every local variable to itself which causes the compiler to warn you about that.

How to set environment variables in bash script?

(. script.sh and source script.sh will first look for script.sh in PATH, so it’s safer to specify the path to script.sh .) When you run a script, it runs in a subshell. Variables are only valid within the context of that subshell. Set them in your .bashrc or .profile and read up on variables and subshells.