简体   繁体   中英

Why does sh/bash set command line parameter values when trying to set environment variable?

A question on basics : While tuning environment variables for a program launched from a script, I ended up with somewhat strange behaviour with sh (which seems to be actually linked to bash) : variable setting seems to mess up with command-line parameters.

Could somebody explain why does this happen?

A simple script:

#! /bin/sh

# Messes with $1 ??
set ANT_OPTS=-Xmx512M
export ANT_OPTS

# Works
# export ANT_OPTS=-Xmx512M

echo "0 = $0"
echo "1 = $1"

When I run this with the upper alternative (set + export), the result is as following:

$ ./test.sh foo
0 = ./test.sh
1 = ANT_OPTS=-Xmx512M

But with lower alternative (export straight), the result is as I supposed:

$ ./test.sh foo
0 = ./test.sh
1 = foo

There is surely logical explanation, I just haven't figured it out yet. Somebody who does have idea?

br, Touko

You should just use ANT_OPTS=-Xmx512M instead of set ANT_OPTS=-Xmx512M .

UPDATE: See here for discussion of set , and the manual .

"set" isn't part of setting variables in Bourne Shell. That should be

ANT_OPTS=-Xmx512m
export ANT_OPTS 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM