Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setenv variable with spaces in launchd.conf?

Tags:

macos

launchd

I'm a Linux user that just recently got a mac. I'm trying to set up my IDE and found out that Macs don't use .bashrc / .bash_profile / etc. for GUI apps. So, if you have a GUI app that needs an environment variable, you're apparently supposed to do it in /etc/launchd.conf

The thing is, this file uses csh style setenv syntax ("setenv key value" instead of "export key='value'") so now that I have a variable that has a space in it, I don't know what to do. Nothing is working. This is what I've tried to test it:

setenv MAVEN_OPTS "-Xms512m -Xmx1024m"
setenv MAVEN_OPTS1 '-Xms512m -Xmx1024m'
setenv MAVEN_OPTS2 (-Xms512m -Xmx1024m)
setenv MAVEN_OPTS3=(-Xms512m -Xmx1024m)
setenv MAVEN_OPTS4 -Xms512m -Xmx1024m
setenv MAVEN_OPTS5 -Xms512m
setenv MAVEN_OPTS6 "$MAVEN_OPTS5 -Xmx1024"
setenv MAVEN_OPTS7 $MAVEN_OPTS5 -Xmx1024
setenv MAVEN_OPTS8 /just/checking
setenv MAVEN_OPTS9="-Xms512m -Xmx1024m"
setenv MAVEN_OPTS10='-Xms512m -Xmx1024m'
setenv MAVEN_OPTS11='-Xms512m\ -Xmx1024m'
setenv MAVEN_OPTS12 '-Xms512m\ -Xmx1024m'
setenv MAVEN_OPTS13 "-Xms512m\ -Xmx1024m"
setenv MAVEN_OPTS14 -Xms512m\ -Xmx1024m

After a reboot only var #5 and #8 survive. (The ones with no spaces.) None of the rest are in my environment.

like image 899
inanutshellus Avatar asked Mar 25 '10 20:03

inanutshellus


People also ask

Can environment variable have space?

Most programs separate their command line arguments with a space. But the PATH environment variable doesn't use spaces to separate directories. It uses semicolons.

What is the difference between set and Setenv?

In short, set command is used for this shell and setenv for this and any subshells. Usually, all system environmental variable such as $HOME, $USER, $MAIL, $PATH, and others are defined using setenv command.

What is Setenv VAR?

SETENVVAR sets the environment variable named LNAME to VALUE . Returns TRUE on success, FALSE on failure (which may be caused by not being able to allocate environment space).

Is export the same as Setenv?

setenv is just export in csh-family shells, as stated in your answer. In modern shells, VAR=asdf does update the environment if VAR was already in the environment.


2 Answers

Try using launchd instead (create plist /Library/LaunchDaemons/java.props.plist):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" 
    "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>java.props</string>
    <key>ProgramArguments</key>
    <array>
        <string>launchctl</string>
        <string>setenv</string>
        <string>JAVA_OPTS</string>
        <string>-Djava.io.tmpdir=/tmp -Dfile.encoding=UTF-8</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>LaunchOnlyOnce</key>
    <true/>
</dict>
</plist>

This will run once and set your environment up. Hope it'll help.

like image 94
Serge ETL Avatar answered Nov 02 '22 17:11

Serge ETL


Add the following line to /etc/launchd.conf (create if it doesn't exist)

setenv MY_VARIABLE My\ value\ with\ spaces

Note that this will only have an effect after rebooting.

To use the new value without having to reboot, additionally run the command in the terminal

launchctl setenv MY_VARIABLE My\ value\ with\ spaces
as patrikha suggested.

Note that this will only have an effect for applications started after running the command. Manipulating /etc/launchd.conf is still necessary to keep the change after rebooting.

like image 34
Random Citizen Avatar answered Nov 02 '22 16:11

Random Citizen