Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

dash: How to export a user function

903 views
Skip to first unread message

James Waldby

unread,
Dec 17, 2011, 3:54:49 AM12/17/11
to
I have some scripts that run ok on Fedora Linux systems,
where /bin/sh is linked to /bin/bash, but not on Ubuntu,
where /bin/sh is linked to /bin/dash.

Under dash, export -f fname gives "Illegal option -f" .
Apparently dash also doesn't support 'declare' or 'typeset', so I
presume a 'set' command with some option will do the job. Dash man
page says "unset [-fv] name ..." will unset and unexport functions
and variables, as -f or -v is specified; if a function can be
unexported, there ought to be a way to export it in the first place.

However, I don't see anything in the man page about how to export
a function. I'd like to use a method that allow scripts that start
with #!/bin/sh to run with /bin/sh linked to either of /bin/bash
or /bin/dash. Any ideas?

--
jiw

Stephane CHAZELAS

unread,
Dec 17, 2011, 6:47:51 AM12/17/11
to
2011-12-17, 08:54(+00), James Waldby:
dash is a very simple and efficient shell that implements the
minimum required to be able to interpret POSIX scripts.

There are very few extensions over POSIX that dash implements
and that's on purpose, so that it remains a very small and
efficient shell to interpret shell scripts.

If you need more than what dash provides, chances are that you
need a programming language like perl or ruby, not a shell.

If you want your script to be run by any POSIX sh, stick to the
POSIX syntax for your script. You can find the details at
http://pubs.opengroup.org/onlinepubs/9699919799/

unset -f is to unset a function, not to unexport it, that's
probably a typo in dash man page. POSIX functions can't be
exported. If you want functions to be available to more than one
script, you can write their definition in a file and have that
file sourced by those scripts, but again if you need to go that
far, chances are that you'd be better of using a programming
language.

--
Stephane

James Waldby

unread,
Dec 17, 2011, 2:13:59 PM12/17/11
to
On Sat, 17 Dec 2011 11:47:51 +0000, Stephane CHAZELAS wrote:

> 2011-12-17, 08:54(+00), James Waldby:
>> [...scripts] run ok on Fedora Linux systems, where /bin/sh is linked
>> to /bin/bash, but not on Ubuntu, where /bin/sh is linked to /bin/dash.
>> Under dash, export -f fname gives "Illegal option -f" . [...]
>> Dash man page says "unset [-fv] name ..." will unset and unexport
>> functions and variables, as -f or -v is specified [...]
>>
>> However, I don't see anything in the man page about how to export a
>> function. I'd like to use a method that allow scripts that start with
>> #!/bin/sh to run with /bin/sh linked to either of /bin/bash or
>> /bin/dash. Any ideas?
...
> If you want your script to be run by any POSIX sh, stick to the POSIX
> syntax for your script. You can find the details at
> http://pubs.opengroup.org/onlinepubs/9699919799/
>
> unset -f is to unset a function, not to unexport it, that's probably a
> typo in dash man page. POSIX functions can't be exported. If you want
> functions to be available to more than one script, you can write their
> definition in a file and have that file sourced by those scripts, but
> again if you need to go that far, chances are that you'd be better of
> using a programming language.

On this new ubuntu system, for the moment I've changed #!/bin/sh in a
handful of scripts to #!/bin/bash. For these scripts, I control them
and the scripts they start, so could modify them to source a file;
but in other cases won't have control.

It appears that for simple functions with single-level quoting,
export + eval is lighter-weight than writing a file and sourcing it.
For example, in dash:

tini > export g='f() { t=$1; printf "%.0f squared is %.0f\n" $t $((t*t))
> }'
tini > echo "$g"
f() { t=$1; printf "%.0f squared is %.0f
" $t $((t*t))
}
tini > dash
tini > eval "$g"
tini > f 7
7 squared is 49

--
jiw
0 new messages