Hot answers tagged

28

You could use envsubst (part of gnu gettext): envsubst < infile will replace all1 environment variables in your file with their corresponding value. 1: one can also replace only certain environment variables, see this question.


18

Process arguments are visible to all users, but the environment is only visible to the same user (at least on Linux, and I think on every modern unix variant). So passing a password through an environment variable is safe. If someone can read your environment variables, they can execute processes as you, so it's game over already. The contents of the ...


11

There are really two types of variable: Environment variables Shell variables To make things more complicated, they both look the same, and a shell variable can be converted to an environment variable with the export command. The env command will show the current set of environment variables. $ myvar=100 $ env | grep myvar $ export myvar $ env | grep ...


10

Instead of passing the password directly through an argument or an environment variable #!/bin/bash #filename: passwd_receiver echo "The password is: $1" use the same argument or environment variable to pass a filename: #!/bin/bash #filename: passwd_receiver echo "The password is: $(< "$1")" Then you can pass either a permission-protected regular ...


7

No, environment variables are easily read too, and leak to child processes. pass it using a pipe.


6

If you happen to have Perl (but not gettext and envsubst) you can do the simple replacement with a short script: $ export INSTANCE_ID=foo; export SERVICE_NAME=bar; $ perl -pe 's/\$([_A-Z]+)/ $ENV{$1} /e' < config.xml <property> <name>instanceId</name> <value>foo</value> </property> <property> <...


5

The $_ value is visible to the child process. How you read it in the child is up to the language you use. e.g. in C #include <stdlib.h> #include <stdio.h> main() { char *x=getenv("_"); printf("%s\n",x); } However a program can not rely on $_ being set because other shells may do different things. e.g. $ env -i /bin/bash -c ./a.out ./a....


4

I recommend you read this article. There is a big difference between shell and environment variables. PATH is one of environment ones, and is already exported (by default), so by changing it, the current shell your using and all the others - any child shells or processes - will be affected. PATH is list of directories that the system will check when ...


4

You can use printenv or env. See this post for the differences: What is the difference between 'env' and 'printenv'?


3

This is not very nice but it works ( echo "cat <<EOF" ; cat config.xml ; echo EOF ) | sh If it was in a shell script it would look like: #! /bin/sh cat <<EOF <property> <name>instanceId</name> <value>$INSTANCE_ID</value> </property> EOF Edit, second proposal: eval "echo \"$(cat config.xml)\""


3

You can use --rcfile to specify an alternative to .bashrc at start up. This may be usable in your setup: /opt/xyq/commercialApp/bin/tool/getEnv > ~/setAppEnv cat ~/setMyEnv >> ~/setAppEnv bash --rcfile ~/setAppEnv If you also want to load .bashrc then this can be added after the cat command. You can then make this into a function runApp() { /...


3

How about this? It starts a new bash, with the new environment variables set. /bin/bash -c '/opt/xyq/commercialApp/bin/tool getEnv > ~/setAppEnv; . ~/setAppEnv; . ~/setMyEnv; bash'


3

Per the man page: envsubst [OPTION] [SHELL-FORMAT] If a SHELL-FORMAT is given, only those environment variables that are referenced in SHELL-FORMAT are substituted; otherwise all environment variables references occurring in standard input are substituted. Where SHELL-FORMAT strings are "strings with references to shell variables in the ...


3

Before calling envsubst your should use export, and use single quotes,to get back VAR1 modified. As in: export VAR1='somevalue' For more details, please see: How to substitute shell variables in complex text files


2

In addition to using Environment directive in the systemd service itself as suggested in this answer, another option is the EnvironmentFile directive. Please note that I am no expert in this area (obviously, I am the author of this question); I am only summarizing to the best of my ability the Fedora Wiki. Feel free to edit/correct me. EnvironmentFile ...


2

sudo doesn't necessarily update the HOME variable for the new user. If you want HOME updated, use the -H or -i options. For example: sudo -Hu newuser bash Alternatively, you can add this line to the /etc/sudoers file to have sudo automatically update HOME and other relevant variables: Defaults env_reset Many distributions already have ...


2

You have to worry about more than just $JAVA_HOME; you also need to set $PATH if you are going to call the commands without an absolute path. ie java and not /opt/java/1.7/bin/java. Now depending on how your script works or how you call java, you have a few options. Bash Script Doing it this way means you do not need to add an extra user #!/bin/bash ...


2

You can do source /path/to/script or . /path/to/script To run the commands in the script in the current bash session. Note: do not call exit from the script in that case since it will finish your current session (likely before you can see the time if the terminal emulator closes on shell exit). On the other hand, if you are only measuring session ...


2

On most systems, you can set environment variables in the file ~/.pam_environment and they will apply to all login methods, even those that don't start a login shell and so don't load .profile. This file is read by the PAM module pam_env, i.e. the PAM configuration file /etc/pam.conf or /etc/pam.d/XXX for the service that logs the user in should mention the ...


2

Use this instead, noting that your entire command must be enclosed within the backticks: HT=`hostname | cut -f 1 -d "."` You could potentially use $() as well: HT=$(hostname | cut -f 1 -d ".") This syntax for command substitution is not supported in certain shells, though, including the original Bourne shell, csh and tcsh. For those shells you will ...


1

The guide that you presented tells you how to set these variables globally - for all users. Since you only want to set these for yourself, you should put them in ~/.bashrc EDIT: As Gilles pointed out in the comment below, .bashrc is intended for interactive settings, and few things can go wrong under some circumstances. Instead, you should put them in ....


1

If nothing else suits, consider the Linux Key Retention service (kernel keyrings). Start at: security/keys.txt. One of the default keyrings can be cloned between parent and child processes. It's not the simplest solution, but it's there and appears to be maintained & used (it was also implicated in an Android bug last year.) I don't know about its "...


1

The machine you're working on seems to have some sort of module system for dynamically making software available, possibly the "Modules" system (link currently dead because SourceForge is down). I'm on a system which uses Modules too: $ type mpicc -ksh93: whence: mpicc: not found $ module load openmpi $ type mpicc mpicc is a tracked alias for /opt/openmpi/...


1

You will need to either type export HISTFILE on the command line prior to starting the python interpreter (or running your python script) or add export HISTFILE to your .bashrc file so that it is automatically exported when you login.


1

The problem was with the use of -i as this option includes the headers in the output. The strange thing is that unless you do echo "$ACCESS_TOKEN" you won't see the headers polluting the REST response coming back. Simply remove -i and it should work.


1

/etc/environment is not a shell script, it's a configuration file for pam_env and has a different syntax. /etc/environment (and the per-user file ~/.pam_environment can only contain simple lines of the form VARIABLE=VALUE. You must not use shell features such as export, quoting or refering to the existing value of a variable with VARIABLE. If you want to ...


1

env is a separate program in most shells, so it prints the environment it received from the shell. The Bourne Shell uses an environment management system based on shell variables. At startup, it imports the environment into the list of shell variables. Creating or modifying shell variables does not modify the environment that the shell sets up for new ...


1

That is not possible (in the running shell). To print an environment variable value you need a command, either use printenv: $ printenv "LANG" LANG=en_US.UTF-8 Which will report the value of the variable as exported to the command's environment. Or, you will use echo (or printf, or print) the value of the variable in the present running shell: $ echo "$...


1

If you're running on a Linux-based system, you can examine a process's environment by reading the /proc/[pid]/environ pseudo-file -- but only if the process is still running and you have permission. Its contents are similar to the output of env or printenv except that variables are separated by null characters rather than newlines. man proc and search for ...


1

You should not have to manually set MANPATH on a Mac. Unlike most Unix-based systems, OS X automatically selects an appropriate search path for man pages based on the contents of PATH. The rules for this are described in the section "Search Path for Manual Pages" in manpath(1). The configuration file for this has been in other locations in the past, but ...



Only top voted, non community-wiki answers of a minimum length are eligible