Alias is not working under Bourne Shell

Hi,

Please assist me why HC alias is not working under this shell?

root@singapore # grep HC /.profile
alias HC='cd /var/tmp/HC/2015/`date +%B`'
root@singapore # . /.profile
Sourcing //.profile-EIS.....
root@singapore # echo $HC

root@singapore # HC
HC: not found
root@singapore # echo $SHELL
/sbin/sh
root@singapore # uname -a
SunOS singpapore 5.10 Generic_147440-19 sun4v sparc SUNW,SPARC-Enterprise-T5120

But, it works when I read the users profile:

root@singapore # bash
root@singapore # HC
bash: HC: command not found
root@singapore # . .profile
Sourcing //.profile-EIS.....
root@singapore # HC
root@singapore # pwd
/var/tmp/HC/2015/November
root@singapore #

Have you tried to execute in command line first to see?

alias HC='cd /var/tmp/HC/2015/`date +%B`'
 HC 

What do you get?

1 Like

The first you seem to run unser sh , the second under bash . What be the results of alias HC in either?

Solaris /bin/sh does not have aliases.
Either have a function in .profile

HC() {
  cd /var/tmp/HC/2015/`date +%B`'
}

Or have the alias (or the function) in .bashrc, that is run when you start a bash.

root@singapore # HC='cd /var/tmp/HC/2015/`date +%B`'
root@singapore # echo $HC
cd /var/tmp/HC/2015/`date +%B`
root@singapore # alias  HC='cd /var/tmp/HC/2015/`date +%B`'
root@singapore # echo $HC
cd /var/tmp/HC/2015/`date +%B`
root@singapore # echo $SHELL
/sbin/sh

---------- Post updated at 09:49 AM ---------- Previous update was at 09:48 AM ----------

root@singapore # HC='cd /var/tmp/HC/2015/`date +%B`'
root@singapore # echo $HC
cd /var/tmp/HC/2015/`date +%B`
root@singapore # alias  HC='cd /var/tmp/HC/2015/`date +%B`'
root@singapore # echo $HC
cd /var/tmp/HC/2015/`date +%B`
root@singapore # echo $SHELL
/sbin/sh

Read MadeInGermany answer, aliases are simply not supported by the legacy Bourne shell which predates functions. There is no point trying to define and execute an alias under /sbin/sh or /bin/sh when running Solaris 10 and older.

In any case, to see what an alias contains, echo is the wrong command, as RudyC already posted, you simply use the alias builtin too (when supported by the shell).

Finally, "echo $SHELL", is also an incorrect way to identify what shell you are running, This command will tell you the user's default shell, i.e. the one defined in the passwd database.

1 Like

Well, echo $SHELL will print the active shell.
I get different results whether i type that inside a zsh bash or sh script, but in the terminal, usualy you are right, it prints the default shell, since most ppl dont change the terminal its shell.

If it is bash or sh, did you try:

shopt expand_aliases

hth

Hi, sea.

I'm not sure what you mean by active shell. I think of the value of environment variable SHELL as the path of the login shell. Here are several invocations of shells and the value of variable SHELL:

vm-jessie ~ $ echo $SHELL
/bin/bash
vm-jessie ~ $ ksh
h ~ $ echo $SHELL
/bin/bash
h ~ $ zsh
\h $(eval "echo ${_MYPSDIR}") $ echo $SHELL
/bin/bash
\h $(eval "echo ${_MYPSDIR}") $ csh
% echo $SHELL
/bin/bash
% tcsh
vm-jessie:~> echo $SHELL
/bin/bash
vm-jessie:~> ps
  PID TTY          TIME CMD
12669 pts/0    00:00:00 bash
13606 pts/0    00:00:00 ksh
13608 pts/0    00:00:00 zsh
13609 pts/0    00:00:00 csh
13610 pts/0    00:00:00 tcsh
13615 pts/0    00:00:00 ps

It worked this way on Debian (as shown), CentOS, Fedora, Slackware, OS X.

Best wishes ... cheers, drl

1 Like

It doesn't, it prints the Shell environment variable which is set at login time to the user's default shell.

This won't work with the Bourne shell which is what the OP is running. The shopt builtin is, unlike aliases, bash specific.

No. The SHELL var, where used/understood just says what shell to use if needed (e.g. .a sub shell created from another process). Doesn't have to be the shell defined in user's passwd entry at all, in fact has better chance of being set by the shell itself.

What shell is running?

echo $0 might be the best hint. Or ps -p $$

Jilliagre is right. E.g. bash sets SHELL to the user's default shell if not yet set.
The old Bourne shell does not do that, but these systems use the login program - that does it.
And you are right, too. Once SHELL is set (automatically or manually), many tools (e.g. shell-terminal) open this shell.
--
Traditionally /sbin contained statically-linked binaries. At that times /usr was a separate partition or even a separate disk. If /usr failed and the kernel could load from / then it was good to have statically linked tools under / , without the need of the /usr/lib/*.so
Indeed Solaris 9 and older have

% ls -l /bin/sh
-r-xr-xr-x   4 root     root       95480 Nov 19  2008 /bin/sh
% ls -l /sbin/sh
-r-xr-xr-x   2 root     root      306216 Nov 19  2008 /sbin/sh
% ldd /bin/sh
        libgen.so.1 =>   /usr/lib/libgen.so.1
        libsecdb.so.1 =>         /usr/lib/libsecdb.so.1
        libnsl.so.1 =>   /usr/lib/libnsl.so.1
        libc.so.1 =>     /usr/lib/libc.so.1
        libcmd.so.1 =>   /usr/lib/libcmd.so.1
        libdl.so.1 =>    /usr/lib/libdl.so.1
        libmp.so.2 =>    /usr/lib/libmp.so.2
        /usr/platform/SUNW,Sun-Fire-V240/lib/libc_psr.so.1
% ldd /sbin/sh
ldd: /sbin/sh: file is not a dynamic executable or shared object

For each fix in one of the shared libraries Sun had to provide a new /sbin/sh (to also contain the fix). This was cumbersome.
Solaris 10 changed to

% ls -l /bin/sh
lrwxrwxrwx   1 root     root          13 Nov  7  2011 /bin/sh -> ../../sbin/sh

And in Solaris 11 Oracle has given up the Bourne shell

% ls -l /bin/sh
lrwxrwxrwx   1 root     root           9 Jun 17  2013 /bin/sh -> i86/ksh93
% ls -l /sbin/sh
lrwxrwxrwx   1 root     root          16 Jun 17  2013 /sbin/sh -> ../bin/i86/ksh93
1 Like

I always thought /sbin to be the "/superuser binaries".
Or is this solaris specific?

The primary purpose of /sbin is system recovery. Of course it is used by the "superuser".
Following that sense /usr/sbin was created with further "superuser" tools (dynamically linked).
BTW Solaris 11 has

% ls -ld /sbin
lrwxrwxrwx   1 root     root          10 Jun 17  2013 /sbin -> ./usr/sbin

And all Solaris have

ls -ld /bin
lrwxrwxrwx   1 root     root           9 Jun 17  2013 /bin -> ./usr/bin

Now even RHEL 7 joins in - what a relief!
The /bin /usr/bin /sbin /usr/sbin "quadriga" is quite common in Unix,
Portable shell scripts should start with

PATH=/bin:/usr/bin:/sbin:/usr/sbin

and then use simple commands.
I have seen a thousand scripts that have adventurous workarounds (e.g. manually trying each command with various pathnames, storing them in variables or even in arrays - disgusting!).

1 Like

Wasnt that called 'the usr change/move'? (rh suse deb)

I would advice for portable scripts to have their shebang adapted to point to the POSIX shell available on the target platform, e.g. on Solaris:

#!/bin/ksh

or even better for stricter compatibility:

#!/usr/xpg4/bin/sh

then have the scripts using this PATH:

PATH=$(getconf PATH):$PATH

and use POSIX commands, options and syntax when applicable. Adding /sbin and /usr/sbin to the PATH makes sense too if the scripts need to run administrative commands.

1 Like

@RudiC

root@rrhrprd1 # alias
autoload='typeset -fu'
command='command '
functions='typeset -f'
history='fc -l'
integer='typeset -i'
local=typeset
nohup='nohup '
r='fc -e -'
stop='kill -STOP'
suspend='kill -STOP $$'

@jlliagre
Noted and agreed.

@MadeInGermany
Noted and agreed.

@Sea,

root@rrhrprd1 # man shopt
No manual entry for shopt.
root@rrhrprd1 # shopt
shopt: not found
root@rrhrprd1 #

---------- Post updated at 11:09 AM ---------- Previous update was at 11:06 AM ----------

Workaround

root@rrhrprd1 # bash
root@rrhrprd1 # alias
alias HC='more /var/tmp/HC/`date +%Y`/`date +%B`/`date +%d`'

I have not changed the default shell for the user.

One more workaround:

1 # Updated .profile file as follows:

root@singapore # tail -2 .profile
#Prabhu # This command has been given here to auto switch to bash shell upon every login without changing the default shell for root user.
/usr/bin/bash
root@singapore # ps -p $$
   PID TTY         TIME CMD
 21886 pts/3       0:00 sh

2 # New session after the above update:

root@singapore # ps -p $$
   PID TTY         TIME CMD
 28542 pts/2       0:00 bash

By doing so, I don't have to type bash every time I login to the server.
Alias also works well in this shell.

Thanks for everyone's contribution.

To avoid the double logout issue resulting from such a call, I would recommend using instead:

exec /usr/bin/bash

What?
Shopt is bash specific and shall therefor not run in his bash?

Oh wait, bourne != bourne again.

But either way, i do not think he's using the 'right' SH, as it is located in /sbin.

What is the output of:

ls -l /sbin/sh

I always thought that shells are (only) in bin.

Anyway, how about writing a function, rather than an alias?
Hth

EDIT: Scratch that, didnt see its in Solaris section (was on mobile)