Skip to contentSkip to conversation

setvar MIA?

DISCUSSION Aragon Gouveia 3 participants 5 messages Jan 11, 2011

lore
Share & tools
lore
#0 lore raw

Hi,

I'm working on making a number of shell scripts cross compatible between FreeBSD and Linux, but one thorn in my side has been dash's lack of a setvar builtin. Does anyone know if this is a work in progress, or a decidedly void feature in dash?

Thanks,
Aragon

#1 lore raw
Quoted 6 lines · Aragon Gouveia

Hi,

I'm working on making a number of shell scripts cross compatible between FreeBSD and Linux, but one thorn in my side has been dash's lack of a setvar builtin. Does anyone know if this is a work in progress, or a decidedly void feature in dash?

Decidedly missing. POSIX doesn't require it. Neither bash nor ksh provides setvar as a builtin, either. And what does setvar do anyways?
Perhaps it is some alias or shell function that you have inherited from
startup files in one of your other shells, but I've never heard of a 'setvar' program. So why bloat dash to include it?

Signature · 2 lines
Eric Blake   [email protected]    +1-801-349-2682
Libvirt virtualization library http://libvirt.org
#2 lore raw

Hi,

Quoted 5 lines · Eric Blake

Decidedly missing. POSIX doesn't require it. Neither bash nor ksh provides setvar as a builtin, either. And what does setvar do anyways?
Perhaps it is some alias or shell function that you have inherited from
startup files in one of your other shells, but I've never heard of a 'setvar' program. So why bloat dash to include it?

It is a builtin in FreeBSD's Bourne shell:

http://www.freebsd.org/cgi/man.cgi?query=sh&apropos=0&sektion=0&manpath=FreeBSD+8.1-RELEASE&format=html

I wasn't sure of its status in POSIX. It is useful for declaring variable variables - tidier than eval and I imagine faster, eg.

index="1"
setvar var_${index} "value"

Will emulate it with a local function - thanks.

Regards,
Aragon

#3 lore raw
Quoted 7 lines · Aragon Gouveia

I wasn't sure of its status in POSIX. It is useful for declaring variable variables - tidier than eval and I imagine faster, eg.

index="1"
setvar var_${index} "value"

Will emulate it with a local function - thanks.

Indeed, it looks like FreeBSD introduced it as shorthand for:

setvar() { eval $1=\$2; }

The speed difference between that function doing an eval and a shell builtin would be in the noise. I don't know why FreeBSD even bothered to pollute the namespace with a builtin like that.

Signature · 2 lines
Eric Blake   [email protected]    +1-801-349-2682
Libvirt virtualization library http://libvirt.org
#4 lore raw
Quoted 9 lines · Eric Blake
Quoted 7 lines · Aragon Gouveia

I wasn't sure of its status in POSIX. It is useful for declaring variable variables - tidier than eval and I imagine faster, eg.

index="1"
setvar var_${index} "value"

Will emulate it with a local function - thanks.

Quoted 7 lines

Indeed, it looks like FreeBSD introduced it as shorthand for:

setvar() { eval $1=\$2; }

The speed difference between that function doing an eval and a shell builtin would be in the noise. I don't know why FreeBSD even bothered to pollute the namespace with a builtin like that.

The setvar builtin was already present and documented in the initial ash. FreeBSD simply inherited it. Dash inherited it too, but it was removed in dash-0.4.14, 3 Apr 2003.

The use of Almquist's additions like this one is certainly questionable; many of them have been removed. However, setvar has been available and documented in /bin/sh in all versions of FreeBSD, which makes removal less likely at this point.

Signature · 1 line
Jilles Tjoelker