Is there a command to dump local variables in a bash script? - bash

I have many scripts where I make use variables within the scripts. While the script is running, can I issue a command within the script to dump all of the variables and their values?

The command env on its own should display all the variables.
The builtin command set on its own will also dump the variables.
Note that inherited variables will also be dumped.

If you are after a way of helping you debug your script, you could put
set -x
At the beginning of your script that will display command traces including how your variables are used.

Related

Make variables exist outside a shell script

I'm trying to make variables exits outside of a shell script without using source.
The variables are declared in shell script with
export varA=3
and I run the script with ./filename.sh
I want
echo $varA
in the terminal to return 3 (i.e. the value of varA). So extend the scope of the variable to outside of the script
To sum up: how do I make the variables inside a shell script exist outside.
Thank you in advance
You can run your script on this way:
. ./filename.sh
This mean when run it will not spawn new shell but run it in current. And variables you set in your script will be available in your shell. This is kind of "source" as mentioned in comments.

How to display jenkins default environment variable from shell command?

I am not able to get output of the Environment-Variable BUILD_URL through Shell command.
Configured Manage Jenkins-->Configure System-->Jenkins Location-->Jenkins URL as http://xxx.xxx.xxx.xxx:8080/
Using below script within Shell command.
#!/bin/bash
echo ${BUILD_URL}
Do i have to set jenkins_url within the script?
In a Shell you can't access environment variables like that. you will need to read here: https://www.digitalocean.com/community/tutorials/how-to-read-and-set-environmental-and-shell-variables-on-a-linux-vps.
What you can do is in the Jenkins's job session you can send a parameter to your script to gain access to the env parameter. in the Shell section you will have access to the environment parameter, in the script itself it'll work differently.
Good luck

Write all variables set in a shell script to file?

Is there a way to output all the variables set in a (zsh/bash-)script? To clarify, I don't want the environment variables, just the ones the script has created.
Of course I could output each and every variable separately, but there's plenty of them... And running "set" gives me all variables.
Best,
Gerhard
I'm not aware of any direct way to do it, but you you could run set or env at the start and at the end of the script and compare them?

Changing the value of a export variable from a bash script

I did the following from a bash shell.
export myPath="/home/user/dir"
Then I verified the value of this by 'echo'ing this value from both shell and a inside a bash script. Both worked fine.
Then I tried setting the value of this variable from inside a script like this.
myPath="/home/user/newdir"
and tried printing this variable from shell. I thought the variable will hold the updated value, but it was showing the old value.
How can I update the value from a script? I am looking to do it without using source if possible.
To make the variables persist after the script finishes, you have to run it using the source command:
When a script is run using source it runs within the existing shell, any variables created or modified by the script will remain available after the script completes. In contrast if the script is run just as filename, then a separate subshell (with a completely separate set of variables) would be spawned to run the script.

How do I set bash environment variables from a script?

I have some proxy settings that I only occasionally want to turn on, so I don't want to put them in my ~/.bash_profile. I tried putting them directly in ~/bin/set_proxy_env.sh, adding ~/bin to my PATH, and chmod +xing the script but though the script runs, the variables don't stick in my shell. Does anyone know how to get them to stick around for the rest of the shell session?
Use one of:
source <file>
. <file>
In the script use
export varname=value
and also execute the script with:
source set_proxy_env.sh.
The export keyword ensures the variable is marked for automatic inclusion in the environment of subsequently executed commands. Using source to execute a script starts it with the present shell instead of launching a temporary one for the script.
Did you try this:
. ~/bin/set_proxy_env.sh
Running it by itself opens a separate subshell (I think) and sets the variable there. But then the binding is lost after exiting back into your shell. The dot at the front tells it to run it within the same shell.
Also, don't forget to export the variables you need like so: export MYVAR=value

Resources