Working with Environment Variables
To list all environment variables and their values use:$ env
To display the value of some particular env var use echo $ENV_VAR_NAME. Example:
$ echo $GOPATH
/home/test_user/dev/go
To set environment variables for the single command:
Example:
$ env GOOS=linux GOARCH=amd64 go build cmd/main.go
From the executable's point of view, the same would have been achieved without using env:
$ GOOS=linux GOARCH=amd64 go build cmd/main.go
$ export GOPATH=/mnt/c/dev/go
export is a bash builtin. export key=value is extended syntax and should not be used in portable scripts (i.e. #! /bin/sh)
What's the difference between set, export and env and when should I use each?
What is the difference between set, env, declare and export when setting a variable in a Linux shell?
If some bash script calls executable which requires some env variables, we also need to use export.Example:
demo.sh:
#!/bin/bash
...
echo
echo Env variables:
go env
export CGO_ENABLED=0
export GOOS=linux
export GOARCH=amd64
echo
echo Env variables:
go env
go build -o './bin/myapp' -v './cmd/main.go'
...gives the output:
Env variables:
[16:33:25][Step 4/7] GOARCH="amd64"
[16:33:25][Step 4/7] GOOS="linux"
[16:33:25][Step 4/7] CGO_ENABLED="1"
...
[16:33:25][Step 4/7]
[16:33:25][Step 4/7] Env variables:
[16:33:25][Step 4/7] GOARCH="amd64"
[16:33:25][Step 4/7] GOOS="linux"
[16:33:25][Step 4/7] CGO_ENABLED="0"
...
How do I add environment variables?
How to set an environment variable only for the duration of the script?
Linux Default Environment Variables
host
No comments:
Post a Comment