This question already has answers here:
Bash variable scope
(7 answers)
Closed 4 years ago.
I am working in Ubuntu Shell, but i want it to work for UNIX also.
Here is my code:
#!/bin/bash
func()
{
x=42
}
x=9
func
echo $x
Why is global x changed to 42 when i changed only local variable x
Is in unix different rule for variable scopes?
Is there a method how to set return value from func to global variable, which can be used later in code?
Thank you!
First, you're not programming Linux; you're actually writing a function for the shell program called bash. Enter info bash on the command line to get documentation for that. For any version of Unix, what happens on the command line depends on which type of shell you're running (there are many; bash is the default for most Linux systems).
Variables in a function which are not declared are global by default. As the comment stated, you can use local to mark the variable as local to the function.
Related
This question already has answers here:
Accessing variables from included files in Ruby
(3 answers)
Closed 6 years ago.
In MATLAB, if one writes a script and runs it, the variables defined in the script are available at the command line prompt. I would like to do something similar with IRB or Pry. I wrote a script called "scratch.rb" which simply reads
x = 0
If I type "irb" in the command line in the same directory as this script and type
> load 'scratch.rb'
This appears to run the Ruby script (as I've confirmed by adding a p command). However, if immediately thereafter I try to 'echo' the variable x by
> x
I get an error
NameError: undefined local variable or method `x' for main:Object
Is it possible to make the variables available interactively in this way?
No it is not. Local variables are scoped within the file.
If you want to bruce force, you can read the file as a string and eval it in the binding of the root environment of irb.
This question already has answers here:
How can I make bash treat undefined variables as errors?
(2 answers)
Closed 6 years ago.
I work with a lot of shell scripts that use bash variables. So, for example, I might have a script like this:
option1="-blah_blah"
option2="-yada_yada"
option3="-whatever"
...
option99="-something_else"
./myCommand "$option1 $option12 $option97 $option45"
I am constantly editing that last command to run various engineering tests. The problem is, sometimes I misspell a variable. In that case, Bash simply substitutes an empty string, and my command does the wrong thing silently.
Is there a way to have Bash throw an exception when I try to use a variable that is not defined?
Use:
set -e # Stop on error. I can't believe that this is not default.
set -u # Stop if trying to use un-initialized variables.
This question already has answers here:
What is indirect expansion? What does ${!var*} mean?
(6 answers)
Closed 8 years ago.
I am writing a shell script (#!/bin/sh) which has a variable VAR which contains the name of another variable FOO, which in turn is set to BAR.
FOO=BAR
VAR=FOO
I want to get the value of the variable named in VAR, so something like:
echo "${$VAR}"
But that does not seem to work. Suggestions?
In Bash:
echo "${!VAR}"
Without Bash (though it works in Bash too):
eval echo "\${$VAR}"
Beware: eval is a very general mechanism that can run you into problems very easily. It works fine here, but be cautious about using it more generally.
This question already has an answer here:
Best way to set environment variables in calling shell
(1 answer)
Closed 8 years ago.
I have a script "set_var.sh" written like this
#!/bin/bash
export NAME=release
export ROOT=/Volumes/name/dev/release
but if I run this set_var.sh from terminal, afterward I issue set command to check variables I could not find NAME and ROOT var be set.
I am wondering what is wrong in my case.
it was set in sub-shell.
you need
source set_var.sh
If you simply run set_var.sh, it runs in its own shell which exits, losing the variables that were set.
If you want to change variables in your interactive shell, you can use:
source set_var.sh
or the shorthand,
. set_var.sh
This will execute the lines of the script as if they were typed into your interactive shell.
Note that when you "source" a file this way, it does not require the "shebang" on the first line.
Note also that this is feature exists in Bourne shell as well, but only in the short-form version.
This question already has answers here:
Does variable=$(...) store the command or the result of the command in POSIX sh
(3 answers)
Closed 2 years ago.
In my .bashrc I'm setting a bash variable to the output of a script
export FOO=`/home/jist/tools/lookup1.pl`
This works great except that the output of that script can change during the day (mainly depending on if I'm on the company's VPN or not). So when I do something with the variable, I want it to re-execute the script and get the updated value. I have no idea how to do this? Can someone please help?
Thanks in advance.
As described in a comment by William:
Make it a function emitting a refreshed value on stdout instead of a variable and always access it as $(FOO)
That means:
# create the function: put this in your .bashrc
FOO() { /home/jist/tools/lookup1.pl "$#"; }
# use the function and store its output: do this whenever you want the current result
currentFooResult=$(FOO)
# or, to just print the result to stdout:
FOO