HPE Community read-only access December 15, 2018
This is a maintenance upgrade. You will be able to read articles and posts, but not post or reply.
Hours:
Dec 15, 4:00 am to 10:00 am UTC
Dec 14, 10:00 pm CST to Dec 15, 4:00 am CST
Dec 14, 8:00 pm PST to Dec 15, 2:00 am PST
System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

how to change shell within the script

 
vz7r1x
Regular Advisor

how to change shell within the script

I have an script which starts with #!/bin/sh but my native shell is ksh. So in the script, I say echo $SHELL after /bin/shell line and shell is ksh.

I just want shell changed for this session only. How can I change my sheel from ksh to bsh within the script?

Thanks
10 REPLIES
James R. Ferguson
Acclaimed Contributor

Re: how to change shell within the script

Hi:

> How can I change my sheel from ksh to bsh within the script?

You don't have to if your script begins with a "she-bang" interpreter line. In HP-uX:

#!/bin/sh

...is equivalent to:

#!/usr/bin/sh

...which is the POSIX shell --- the HP-UX standard. You login shell (as defined in '/etc/passwd') might be the Korn shell ('/usr/bin/ksh') but your script will run as the POSIX shell when your interpreter line reads:

#!/usr/bin/sh

(or)

#/bin/sh

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: how to change shell within the script

> I just want [...]

I don't know what that means. If the script
starts with "#!/bin/sh", then that's what
gets run to interpret the script.

> So in the script, I say echo $SHELL after
> /bin/shell line and shell is ksh.

If that bothers you, then you could set SHELL
to some other value (in your script). I
don't know how that will help you, but you
can do it.

Is there some actual problem which you are
trying to solve?
Bill Hassell
Honored Contributor

Re: how to change shell within the script

EVERY script must start with #! on the first line followed by the interpreter you want to use for your script. These are examples of different interpreters:

#!/usr/bin/sh
#!/usr/bin/ksh
#!/usr/bin/awk
#!/usr/local/bin/bash

and so on. A script is a set of instructions that are designed to be run by a specific program. Whether the program is also your shell or some other program is not important. If you do not have the #! line in your script, then you script will be run by the current shell -- not a good idea at all. Always specify the #! line for every script.


Bill Hassell, sysadmin
Steven Schweda
Honored Contributor

Re: how to change shell within the script

> EVERY script must start with #! [...]

> If you do not have the #! line [...]

Make up your mind. (Or else don't say "must"
when you really mean "should".)

Re: how to change shell within the script

>I say echo $SHELL after /bin/sh line and shell is ksh.

SHELL is set by login(1). And just about the only program that looks at $SHELL is make.
isunrise_1
Occasional Visitor

Re: how to change shell within the script

bsh is posix shell
James R. Ferguson
Acclaimed Contributor

Re: how to change shell within the script

Hi (again):

> bsh is posix shell

Yes, it has Posix compliance. BUT: In HP-UX, the standard shell in '/usr/bin/sh' and in '/sbin/sh' is called the "Posix" shell. The important part is that while the 'bash' or the 'ksh' or other shell may be declared as a user's default shell, you should NEVER change root's default login shell. For root, only the statically linked '/sbin/sh' will allow you to reboot and startup a system (when '/usr' and the dynamic libraries every other shell needs) isn't mounted.

Regards!

...JRF...
mvpel
Trusted Contributor

Re: how to change shell within the script

Actually, Bill, not every script must start with #!.

There's a little trick you can use to get the shell to use a PATH search to find the Perl interpreter for you:

----
:
eval 'exec perl -S $0 ${1+"$@"}'
if $running_under_some_shell;

print "Hello world!\n";
----

I use this when some systems have Perl in /usr/local/bin and others have it in /usr/bin, when I haven't gotten around to symlinking.

The colon sends the script to sh, which runs the "eval/exec" line to fire up Perl on the same file, and then Perl ignores that line because "$running_under_some_shell" on the following line is not set, and proceeds down to the rest of the Perl script.
James R. Ferguson
Acclaimed Contributor

Re: how to change shell within the script

Hi (again):

> mvpel: Actually, Bill, not every script must start with #!. There's a little trick you can use to get the shell to use a PATH search to find the Perl interpreter for you:

A less obtuse way is simply:

#!/usr/bin/env perl

(or):

#!/usr/bin/env ksh

In which case we could still say that "every script must start with #!" :-)

Regards!

...JRF...
vz7r1x
Regular Advisor

Re: how to change shell within the script

Thanks folks. I was really overwhelmed by the storm of scripting knowledge.