Export Not Working in My Shell Script

Why isn't export working from bash script

When you run /tmp/example.bash &, you set the environment in the sub-shell, but that does not affect the parent shell that ran it.

You need to (a) remove the sleep 1000 and (b) use the . command or (in Bash or a C shell) the source command to read the file as part of the current process:

sed -i.bak '/sleep/d' /tmp/example.bash  # GNU or BSD sed
. /tmp/example.bash
echo $PARAM

shell/bash while read export variable not working for subtitute

The order of expansions are described in POSIX:

The order of word expansion shall be as follows:

  1. Tilde expansion (see Tilde Expansion), parameter expansion (see Parameter Expansion), command substitution (see Command Substitution), and arithmetic expansion (see Arithmetic Expansion) shall be performed, beginning to end. See item 5 in Token Recognition.

  2. Field splitting (see Field Splitting) shall be performed on the portions of the fields generated by step 1, unless IFS is null.

  3. Pathname expansion (see Pathname Expansion) shall be performed, unless set -f is in effect.

  4. Quote removal (see Quote Removal) shall always be performed last.

For Parameter Expansion, the tl;dr is:

${parameter}

The value, if any, of parameter shall be substituted.

You will notice that the definition is not recursive or multi-pass. There is no "5. Go back to step 1 and repeat until no further expansions can be performed", or "The value, if any, of parameter shall be substituted, and the process repeated until no more parameter expansions can be found."

This means that export $vars will expand $vars in step 1, but will not repeat that step and therefore leave $ENV alone.

This is also how basically all other programming languages work, e.g. Java:

String foo="bar";
String bar="baz";
// Prints `bar` without recursively resolving it into `baz`
System.out.println(foo);

exporting PATH not working in a bash shell on linux

you have set the PATH environment variable only for your current bash session. You need to add the line PATH=$PATH:/var/test into ~/.bashrc so that it works for any bash shell.

Just run the following command to put it into your rc(run commands) file (rc files contain startup information for a command(initialization)):

echo "PATH=$PATH:/var/test" >> ~/.bashrc

More info: https://en.wikipedia.org/wiki/Run_commands

https://superuser.com/questions/789448/choosing-between-bashrc-profile-bash-profile-etc

exporting a variable makes it available only in child processes spwaned/started from that bash shell.

As an example:

$ export var=abcd
$ sh
$ echo "$var"
abcd
$ exit
$ echo "$var"
abcd
$

sh is the child process of bash hence it gets the value of var, since you open a new bash which is a different process altogether it does get the PATH value.

Execute a Response of a command in bash

You can just use sed:

`sed -n -e 's/^.*\(\(export SECRET_KEY\).*\)/\1/p' test.txt`

Be careful, the export that you made doesn't work.

export SECRET_KEY= '321321'
export: not an identifier: 321321

Use:

export SECRET_KEY=321321

Why variables that I export in bash script does not work on EC2?

It is run on its own bash process which dies at the end of your script.

Exported variables are preserved only for the lifetime of your script and they are also visible from child processes of your script.



Related Topics



Leave a reply



Submit