This question already has answers here:
Environment variable is not set on terminal session after setting it with "os" package
(2 answers)
How to set environment variables that last
(4 answers)
Closed 4 days ago.
I'm trying to use Go to set an env variable with the following code:
package main
import (
"fmt"
"os"
)
func main() {
os.Setenv("S3key", "abc")
fmt.Println("S3key:", os.Getenv("S3key"))
}
When I run go run temp.go, it prints out S3key: abc as expected, but when I check with printenv in the terminal, it doesn't show this env variable at all.
It appears that my script doesn't actually change the env variable as it should. Any pointers on why this is happening and how I can fix it?
Thanks!
Related
This question already has answers here:
Difference between sh and Bash
(11 answers)
$BASH_VERSION reports old version of bash on macOS, is this a problem that should be fixed?
(4 answers)
Closed 10 months ago.
I have some code that looks like this:
export AWS_REGION='us-east-1'
export CLUSTER_NAME='my_cluster'
export PROJECT_NAME='my_project'
source ./utils.sh
...additional functions...
annotate_cluster # comes from utils.sh
annotate_cluster, which comes from utils.sh, relies on the environment variable PROJECT_NAME. However, when I run it, it complains _utils.sh: line 60: FOO-${PROJECT_NAME^^}: bad substitution. Why can it not access the environment variable I have set?
This question already has answers here:
Application auto build versioning
(7 answers)
Closed 4 years ago.
I'm building Golang project on gitlab CI and I'd like to pass $CI_COMMIT_TAG value to compiler. With g++ it would be something like g++ -DCI_COMMIT_TAG=$CI_COMMIT_TAG .... Is there anything simillar for go build command?
main.go
package main
var YOUR_VARIABLE = ""
func main() {}
Shell command: go build -ldflags "-X main.YOUR_VARIABLE=$your_variable" main.go
This question already has answers here:
How to set environment variables in Python?
(19 answers)
Reading and writing environment variables in Python? [duplicate]
(4 answers)
set environment variable in python script
(3 answers)
Closed 5 years ago.
In terminal (bash) on OSX I can set an environment variable, using the syntax export VARNAME=1 or export VARNAME="hello"; and that persist as long as the session is running, or until the terminal window is closed.
What would be the equivalent form, to do the same via Python3? I would like to avoid to call Popen just to set a global variable.
Also I need this variable only for the purpose to run my python code; once the script is done, I do not need it anymore; so even if it last only for the lifespan of my script running, it is acceptable.
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.
This question already has answers here:
getpasswd functionality in Go?
(11 answers)
Closed 7 years ago.
I'm writing a command line tool with Go and one of the command will query the user for its username and password (and save it to a config file inside the home directory).
For now I couldn't realize how to replace the typed password to '*' or even not to type anything as a lot of command line tools are doing.
How does can this be done when using golang?
Type this stuff into Google before you come here.
you can do this by execing stty -echo to turn off echo and then
stty echo after reading in the password to turn it back on
OR
Just saw a mail in #go-nuts maillist. There is someone who wrote quite
a simple go package to be used. You can find it here:
https://github.com/howeyc/gopass
It something like that:
package main
import "fmt" import "github.com/howeyc/gopass"
func main() {
fmt.Printf("Password: ")
pass := gopass.GetPasswd()
// Do something with pass }
Taken from another Stackoverflow post.