Skip to contentSkip to conversation

Replacement for declare in dash?

DISCUSSION Alexander Huemer 4 participants 5 messages Oct 16, 2013

lore
Share & tools
lore
#0 lore raw

Hi,

I wrote a shell script on a non-Debian system and assumed, in my greenness, that testing it with #!/bin/bash --posix would ensure that the script would run under dash too. I was wrong. Of course that is the fault of bash and not dash.
Here is a simplified form of the script:

#!/bin/dash
# This works when run with #!/bin/bash --posix

func() {
cat /proc/1/environ
}

sudo sh -c "$(declare -f func); func"

Can I do anything equivalent with dash?
I did not find anything in the docs, but hope dies last.
Of course I can swap the function out in a seperate script, but I'd like to avoid that.
Thanks in advance for all ideas.

Kind regards,
-Alex

#1 lore raw
Quoted 11 lines · Alexander Huemer

Hi,

I wrote a shell script on a non-Debian system and assumed, in my greenness, that testing it with #!/bin/bash --posix would ensure that the script would run under dash too. I was wrong. Of course that is the fault of bash and not dash.
Here is a simplified form of the script:

#!/bin/dash
# This works when run with #!/bin/bash --posix

Quoted 7 lines

func() {
cat /proc/1/environ
}

sudo sh -c "$(declare -f func); func"

Can I do anything equivalent with dash?

No.

Quoted 1 line

I did not find anything in the docs, but hope dies last.

This is even ugly in bash because you're abusing a function as a variable. And the shell you get with "sudo sh" depends on your system configuration.
You can embed commands using a here-doc instead of func()...

sudo /bin/dash << "EOF"
cat /proc/1/environ

# ...
EOF
Quoted 2 lines

Of course I can swap the function out in a seperate script, but I'd like to avoid that.

...but this seems to be the right thing to do rather than embedding a script inside a script.

Signature · 1 line
Guido Berhoerster
#2 lore raw
In reply to Alexander Huemer
Quoted 6 lines · Alexander Huemer

Hi,

I wrote a shell script on a non-Debian system and assumed, in my greenness, that testing it with #!/bin/bash --posix would ensure that the script would run under dash too. I was wrong. Of course that is the fault of bash and not dash.

Maybe you should have verified your assumptions first. That is simply not what the --posix option does. Posix mode makes bash a superset of Posix; it's not a nothing-but-what-posix-specifies mode.

Chet

Signature · 3 lines
``The lyf so short, the craft so long to lerne.'' - Chaucer
		 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    [email protected]    http://cnswww.cns.cwru.edu/~chet/
#3 lore raw
Quoted 11 lines · Chet Ramey
Quoted 6 lines · Alexander Huemer

Hi,

I wrote a shell script on a non-Debian system and assumed, in my greenness, that testing it with #!/bin/bash --posix would ensure that the script would run under dash too. I was wrong. Of course that is the fault of bash and not dash.

Maybe you should have verified your assumptions first. That is simply not what the --posix option does. Posix mode makes bash a superset of Posix; it's not a nothing-but-what-posix-specifies mode.

If you want a shell that does nothing-but-what-posix-specifies, use posh from the Debian project.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#4 lore raw
In reply to Chet Ramey
Quoted 11 lines · Chet Ramey
Quoted 6 lines · Alexander Huemer

Hi,

I wrote a shell script on a non-Debian system and assumed, in my greenness, that testing it with #!/bin/bash --posix would ensure that the script would run under dash too. I was wrong. Of course that is the fault of bash and not dash.

Maybe you should have verified your assumptions first. That is simply not what the --posix option does. Posix mode makes bash a superset of Posix; it's not a nothing-but-what-posix-specifies mode.

Thanks for your answer and also for the answers of Guido Berhoerster and Eric Blake. That's correct, my assumption was wrong. I'll try to use the cat << trick, let's see if that works, I won't assume anything at the moment ;) Also thanks for the hint regarding posh, I didn't know that.

Kind regards,
-Alex