Questions tagged [bourne-shell]

The Bourne shell is a historical implementation of /bin/sh

The Bourne shell was the implementation of /bin/sh on Unix systems for a long time, from Version 7 Unix in 1977 to the the early 2000s. It was the starting point for the POSIX shell standard, which builds on the Bourne shell adds a few features, mostly from ksh. It has now mostly been phased out in favor of other implementations that follow the POSIX standard, such as ksh , bash and various flavors of ash (e.g. . The Bourne shell is not free software and thus is not available on Linux or BSD.

Use this tag only for questions about genuine Bourne shells. For questions about Bourne/POSIX-style shells in general, use .

54 questions
43
votes
4 answers

Arrays in Unix Bourne Shell

I am trying to use arrays in Bourne shell (/bin/sh). I found that the way to initialize array elements is: arr=(1 2 3) But it is encountering an error: syntax error at line 8: `arr=' unexpected Now the post where I found this syntax says it is for…
SubhasisM
  • 533
  • 1
  • 4
  • 6
32
votes
4 answers

How to break a long string into multiple lines in the prompt of read -p within the source code?

I am writing an installation script that will be run as /bin/sh. There is a line prompting for a file: read -p "goat may try to change directory if cd fails to do so. Would you like to add this feature? [Y|n] " REPLY I would like to break this long…
Mateusz Piotrowski
  • 4,623
  • 5
  • 36
  • 70
28
votes
3 answers

How can I create an arithmetic loop in a POSIX shell script?

I know how to create an arithmetic for loop in bash. How can one do an equivalent loop in a POSIX shell script? As there are various ways of achieving the same goal, feel free to add your own answer and elaborate a little on how it works. An example…
Vlastimil Burián
  • 27,586
  • 56
  • 179
  • 309
20
votes
5 answers

Is there an "in" operator in bash/bourne?

I’m looking for an “in” operator that works something like this: if [ "$1" in ("cat","dog","mouse") ]; then echo "dollar 1 is either a cat or a dog or a mouse" fi It's obviously a much shorter statement compared to, say, using several "or"…
mrjayviper
  • 1,971
  • 3
  • 25
  • 41
19
votes
3 answers

Use of ^ as a shell metacharacter

I wrote a small script today which contained grep -q ^local0 /etc/syslog.conf During review, a coworker suggested that ^local0 be quoted because ^ means "pipe" in the Bourne shell. Surprised by this claim, I tried to track down any reference that…
Greg Hewgill
  • 7,003
  • 2
  • 31
  • 34
14
votes
3 answers

Is "${PS1-}" valid syntax and how does it differ from plain "$PS1"?

I am looking at a script that has: if [ "${PS1-}" ]; then That trailing - bugs me a bit because it doesn't seem to Posix or Bash standard syntax. It this some arcane syntax that has been around forever, or is it a typo? Any references to standards…
Gregor
  • 1,219
  • 10
  • 16
13
votes
1 answer

IFS null is not the same as unset IFS?

I read a great question on here called Understanding IFS. I was surprised because the answers and comments quote POSIX which states that IFS= is not the same as unsetting IFS. If you unset IFS, apparently the default value is used. If you make IFS…
test
  • 579
  • 1
  • 6
  • 13
9
votes
1 answer

Trying to write a shell script that keeps testing a server remotely, but it keeps falling in else statement when I logout

Trying here to write a shell script that keeps testing my server and email me when it becomes down. The problem is that when I logout from ssh connection, despite running it with & at the end of command, like ./stest01.sh &, it automatically falls…
Baraujo85
  • 698
  • 7
  • 29
7
votes
1 answer

Does Bourne Shell have a regex validator?

I am on a closed network (i.e. no connectivity to the internet). I have a bourne shell script that asks for the user to enter a regular expression for use with grep -P. Generally speaking, I like to do some form of input validation. Is there a way…
Scottie H
  • 644
  • 3
  • 11
5
votes
2 answers

bourne shell if [ -e $directory/file.$suffix ]

#!/bin/sh CONFIG_DIR="/var/opt/SUNWldm/" read option if [ $option -eq 9 ]; then ret=1 elif [ -e ${CONFIG_DIR}file.xml.${option} ]; then echo "TRUE" fi I have the above code in a while loop to present a list of options. Unfortunately…
BitsOfNix
  • 5,057
  • 2
  • 25
  • 34
4
votes
4 answers

Getting search results for Bourne shell

So, there are many types of shells in Linux... Different Types of Shells in Linux The Bourne Shell (sh) ... The GNU Bourne-Again Shell (bash) ... The C Shell (csh) ... The Korn Shell (ksh) ... The Z Shell (zsh) I have a project where I have to put…
pgr
  • 151
  • 3
4
votes
2 answers

Is it possible to "protect" an IFS character from field splitting?

In a POSIX sh, or in the Bourne shell (as in Solaris 10's /bin/sh), is it possible to have something like: a='some var with spaces and a special space' printf "%s\n" $a And, with the default IFS, get: some var with spaces and a special space That…
muru
  • 69,900
  • 13
  • 192
  • 292
4
votes
2 answers

SH: How to make vars from one script available in the main script?

I have two scripts that need to run and both require the same variables set the same way. As a result I figured I'd break the setting of the variables out into a separate script. However, I can't seem to get this to work right where the variables…
Doug
  • 439
  • 1
  • 4
  • 11
4
votes
1 answer

"$@" expansion for user defined variables

I'm trying to get a (bourne shell) variable to expand like "$@" so it produces multiple words with some having preserved spaces. I've tried defining the variable in many different ways but still can't get it to work: #!/bin/sh n=\"one\ two\"\…
user126144
  • 43
  • 2
4
votes
1 answer

Variable scope in while-read-loop on Solaris

Could someone please explain to me why my while loop seems to have an internal scope? I've seen multiple explanations online but they all have to do with pipes. My code has none. The code: #!/bin/sh while read line do echo "File contents:…
Joel Cochran
  • 43
  • 1
  • 3
1
2 3 4