Wednesday 8 August 2007

Bash says: I am different from myself!

Dear lazyweb, is this a bug? Should I fill this?

$ sh -x rpm/build_latest_srpm foo 2>&1 | grep -E '\'
+ alias 'rpmbuilder=rpmbuild --define "_topdir /tmp/rpmbuildarea/foo/redhat"'
+ rpmbuild --define '_topdir /tmp/rpmbuildarea/foo/redhat' -bs foo.spec
$ sh rpm/build_latest_srpm foo 2>&1 | grep -E '\'

$ bash -x rpm/build_latest_srpm foo 2>&1 | grep -E '\'
+ alias 'rpmbuilder=rpmbuild --define "_topdir /tmp/rpmbuildarea/foo/redhat"'
+ rpmbuilder -bs foo.spec
rpm/build_latest_srpm: line 46: rpmbuilder: command not found
$ bash rpm/build_latest_srpm foo 2>&1 | grep -E '\'
rpm/build_latest_srpm: line 46: rpmbuilder: command not found

$ rpm/build_latest_srpm foo 2>&1 | grep -E '\'
rpm/build_latest_srpm: line 46: rpmbuilder: command not found

$ type sh
sh is hashed (/bin/sh)
$ type bash
bash is hashed (/bin/bash)
$ ll /bin/*sh
-rwxr-xr-x 1 root root 677184 2006-12-11 23:20 /bin/bash
lrwxrwxrwx 1 root root 21 2006-08-17 21:05 /bin/csh -> /etc/alternatives/csh
-rwxr-xr-x 1 root root 80200 2007-02-02 09:34 /bin/dash
lrwxrwxrwx 1 root root 4 2006-12-19 15:02 /bin/rbash -> bash
lrwxrwxrwx 1 root root 4 2006-12-19 15:02 /bin/sh -> bash
lrwxrwxrwx 1 root root 13 2006-08-17 20:58 /bin/tcsh -> /usr/bin/tcsh


The script has /bin/bash in the shabang.

I tried to reproduce with another simpler script, and I observed that the issue happens when the script is processed by bash. With a /bin/sh shabang, it didn't since the command processing the script was sh, also all sh commands were ok.

I also suspect that it has to do with this paragraph from the bash manual:


Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using
shopt (see the description of shopt under SHELL BUILTIN COMMANDS below).


... but different and (I would say) worse behaviour than the sh invocation should not happen.

The test script follows:

#!/bin/bash

alias rpm-build="echo \"I:\""
alias dpkg-select="dpkg --get-selections \"ls\*\""

echo "alias1:" && rpm-build test
echo "alias2:" && dpkg-select

Test and enjoy. Before you ask, yes, I use bash functionality.

Update: Also using set -i just before the alias definition and keeping it during the expansion does not help.

3 comments:

Anonymous said...

The different behavior seems like a bug. I suspect it shouldn't expand aliases when running as sh either.

Have you considered writing the script using shell functions?

Anonymous said...

I worked around the issue and dropped the aliasing...

EddyP

Philipp Kern said...

Please remember that bash has different semantics when called as sh. (e.g. POSIX mode activated by default) See bash's man page for details.