Variables set in ~ / .bashrc and access to them in shellscript

I have it at the very top of my .bashrc, before returning for non-interactive shells

FOO="BAR"; export FOO echo "HELLO WORLD" # If not running interactively, don't do anything [ -z "$PS1" ] && return 

I have a test.sh script in my home directory with this:

 #!/bin/bash echo "A" echo $FOO echo "B" 

I execute test.sh. Output:

 A B 

2 Questions:

  • Why can't I see the value of $ FOO?
  • Why can't I see "HELLO WORLD"?

edit: I thought the C #! / bin / bash script launches a sub-net that will call .bashrc again, right?

edit: Even if I call the script from another host, I will not see any values. Even then, .bashrc will be executed.

 ssh remotehost "/home/username/test.sh" 
+4
source share
4 answers

.bashrc automatically created only for interactive shells without logging in. Often you bet . .bashrc . .bashrc at the beginning of your .bash_login file to ensure that .bashrc will be used for both interactive and inactive interactive shells.

.bashrc is not automatically used for non-interactive shells, for example, running when the shell script is executed.

Since you are exporting FOO from .bashrc , the fact that test.sh sees FOO with a null value tells me that you are using a script from the login shell. Print echo $FOO from the BAR command line? I would be surprised if this happened, but test.sh not.

+4
source

When you logged in again, bashrc was launched in your new login shell, and the script shell inherited the variable from the login shell. What “environment variable” means: it is a variable that is passed to child processes.

Scripts are executed in a separate process, but I would not use the word "subshell" for them. I reserve this for things like ( cmd1 ; cmd2 ) , which creates a subshell to start the material in parentheses. Running the shell script calls the new exec interpreter, which is an important difference, as shown here:

 x=foo # not exported export y=bar ( # This subshell is a copy of the existing shell process, including the # non-exported variables, so x is still foo here. echo x is $x echo y is $y # But it a copy, so modifications do not propagate to the parent. x=blah y=blah ) | sed 's/^/SUBSHELL: /' # Back in the parent shell, x and y have not become blah. echo PARENT: x is $x echo PARENT: y is $y # This is not a "subshell" but a new shell. It inherits the exported # variable y, but not x. Running a shell script resembles this. sh -c 'echo x is $x ; echo y is $y' | \ sed 's/^/NEWSHELL: /' 
+3
source

The answer is simple. The solution is in the correct order:

Why can't I see "HELLO WORLD"?

Note that .bashrc will not be processed on every command or when subclasses are run. It will be processed only in the new shell. On the man page:

When an interactive shell that is not a login shell is launched, bash reads and executes commands from / etc / bash.bashrc and ~ / .bashrc, if these files exist.

So, I think your .bashrc will not be processed. Have you tried source ~/.bashrc before executing your script? You can also just open a new terminal.

Why can't I see the value of $ FOO?

If bashrc gets parsed, it will work as expected.


General advice. If you use a variable on the command line, such as echo , use parentheses around the variable name to make sure that the contents of the variable will not be treated as a shell option. So

 echo $FOO 

it should be

 echo "$FOO" 
+1
source

From my man bash :

If the program is a file starting with C # !, the remainder of the first line indicates the interpreter for the program. The shell executes the specified interpreter on operating systems that do not process this executable format. Arguments to the interpreter consists of one optional argument following the name of the translator in the first line of the program, and then the name of the program, followed by the command arguments, if any.

Therefore, I think that a good lawyer can argue that bash can interpret bash and therefore does not execute another interpreter.

+1
source

Source: https://habr.com/ru/post/1487337/


All Articles