13

How can we set environment variables for another user on a machine? I want to run some script with sudo -u xyz but I need to set some environment variables before running the script for xyz user which is different from my login.

2 Answers 2

35

You can add VAR=VALUE between the sudo -u xyz and the script. Example

sudo -u xyz LANG=C LD_LIBRARY_PATH=/usr/local/lib some_script.sh
Sign up to request clarification or add additional context in comments.

Comments

0

From the env(1) man page:

NAME
       env - run a program in a modified environment

SYNOPSIS
       env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]

DESCRIPTION
       Set each NAME to VALUE in the environment and run COMMAND.

1 Comment

can you elaborate on your answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.