How do you run a group of commands with a different environment without affecting the script?
IntermediateConceptArguments, I/O, here-docs, process substitution and subshells
Answer
AWS_PROFILE=prod AWS_REGION=ap-south-1 aws s3 ls # one command
env -i PATH=/usr/bin:/bin HOME="$HOME" ./build.sh # clean environment
( export KUBECONFIG=~/.kube/prod; kubectl get nodes; kubectl get pods -A ) # subshell
The VAR=value cmd prefix does not work for builtins in all cases and does not apply to commands after a pipe. env -i is a great way to reproduce "works in my shell, fails in cron" issues.
More on Arguments, I/O, here-docs, process substitution and subshells
How does `getopts` work? Show a complete example.IntermediateWhat is the difference between `getopts` and `getopt`?AdvancedExplain file descriptors 0, 1 and 2 and the main redirection operators.BeginnerWhy is `cmd 2>&1 > file` different from `cmd > file 2>&1`?AdvancedWhat is a here-document, and what is the difference between `<<EOF`, `<<'EOF'` and `<<-EOF`?BeginnerWhat is a here-string (`<<<`)?Intermediate