1748185 Members
4376 Online
108759 Solutions
New Discussion юеВ

Re: What does "$-" mean?

 
SOLVED
Go to solution
Dan Tingley
Occasional Contributor

What does "$-" mean?

In a script, there is a line:
if test "`echo $- |grep i`"

I don't know what is that for. And I run it out side the script, the answer is:

# echo $-
isum

while running inside the script, it is
# echo $-
vxuh
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

Re: What does "$-" mean?

Hi Dan:

The '-' shell variable ('$-') is the list of single letter options currently set. You see this when you do:

# echo $-

Regards!

...JRF...
Solution

Re: What does "$-" mean?

so in the example you give, the code:

if test "`echo $- |grep i`"

is basically a test to see if the code is being called in an interactive shell (see the Options section of the sh-posix man page)

HTH

Duncan

I am an HPE Employee
Accept or Kudo
Peter Nikitka
Honored Contributor

Re: What does "$-" mean?

Hi,

... and if you modify that script, use
case "$-" in
*i*) ...
;;
easc

instead - no need for an extra process for handling this flag.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Bill Hassell
Honored Contributor

Re: What does "$-" mean?

As mentioned, $- reports the state of shell set options. Long ago and far away (version 7.0 I think) this was commonly coded into /etc/profile in conjunction with the incredibly obtuse ksh incantation to identify the environment.

Since the sample code is detecting the "i" (interactive) option for the current shell, the modern way is to replace the whole line:

if test "`echo $- |grep i`"

with

if tty -s

Much more readable and avoids the use of the deprecated "test" construct (use [[ ]]).


Bill Hassell, sysadmin
Peter Nikitka
Honored Contributor

Re: What does "$-" mean?

Hi,

a replacement, using only builtins for
if tty -s
...

would be
if [ -t 1 ]
...

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Dan Tingley
Occasional Contributor

Re: What does "$-" mean?

Do they work in Korn shell?
James R. Ferguson
Acclaimed Contributor

Re: What does "$-" mean?

Hi:

> Do they work in Korn shell?

Well, the easy way to find the answer is to try it!

Yes, the tests for interactivity work in the Korn shell. On HP-UX, however, your standard shell is the Posix one ('/sbin/sh' for root; and '/usr/bin/sh' for non-root users).

Regards!

...JRF...