Questions tagged [sh]

sh is the standard Unix shell since Version 7 Unix. POSIX has standardized shell behavior based on the Bourne Shell, and portable shell scripts should conform to the standardized syntax. Use this tag for questions that apply to Bourne/POSIX-style shells. For shell scripts with errors, please check them in http://shellcheck.net before posting here.

sh, aka the Bourne Shell, is the standard Unix shell since v7 Unix, in standard location /bin/sh.

POSIX has since standardized shell behavior based on the Bourne Shell, and portable shell scripts should conform to the standardized syntax.

The most common successor to the Bourne Shell (sh) is - The Bourne-Again SHell and many Unix-like operating systems have /bin/sh linked to the bash executable; when bash is called as sh, it runs in a (mostly) POSIX-compliant mode.

The Stack Overflow tag wiki has a large FAQ section; many of the questions and answers there also pertain to (many variants of) sh.

The (Debian Almquist shell) is also used as a small and fast POSIX-compliant shell for running shell scripts; Debian and Ubuntu systems have /bin/sh linked to the dash executable.

Tools

  • checkbashisms (from the devscripts package) is a useful tool for ensuring that a shell script is POSIX-compliant and does not use features not specified by POSIX.

  • ShellCheck is a web application that analyses shell scripts and checks for common mistakes (both syntax and semantic errors) as well as compliance with the POSIX standard, e.g., it highlights unquoted variables and Bashisms (if sh is used in the shebang). It can also be installed as an off-line command-line tool; see the repository in GitHub for more information.

8493 questions
67
votes
11 answers

How do I get the effect and usefulness of "set -e" inside a shell function?

set -e (or a script starting with #!/bin/sh -e) is extremely useful to automatically bomb out if there is a problem. It saves me having to error check every single command that might fail. How do I get the equivalent of this inside a function? For…
Robie Basak
  • 6,492
  • 2
  • 30
  • 34
67
votes
2 answers

Dash double semicolon (;;) syntax

I'm trying to find a way to run multiple commands in parallel in sh and wait for it completion. I've found that following doesn't work (sh: 1: Syntax error: ";" unexpected): sh -c '(sleep 3 && echo 1) & ; (sleep 3 && echo 2) & ; wait' But this…
valodzka
  • 5,535
  • 4
  • 39
  • 50
65
votes
11 answers

getting error /usr/bin/env: sh: No such file or directory when running command play

I am a beginner on Play framework . I just extract Play framework files and extracted them and gave the path of play directory in $PATH global variable. After this when I run the the command on ubuntu play help, its giving me below…
mathlearner
  • 7,509
  • 31
  • 126
  • 189
63
votes
3 answers

Shell script: Run function from script over ssh

Is there any clever way to run a local Bash function on a remote host over ssh? For example: #!/bin/bash #Definition of the function f () { ls -l; } #I want to use the function locally f #Execution of the function on the remote machine. ssh…
Mr.Eddart
  • 10,050
  • 13
  • 49
  • 77
60
votes
1 answer

What is the meaning of set -o pipefail in Bash Script?

What is the meaning of set -o pipefail in the beginning of the shell script ?
Raja G
  • 5,973
  • 14
  • 49
  • 82
60
votes
3 answers

Assigning the output of a command to a variable

I am new with unix and I am writing a shell script. When I run this line on the command prompt, it prints the total count of the number of processes which matches: ps -ef | awk '/siebsvc –s siebsrvr/ && !/awk/ { a++ } END { print a }' example, the…
user3114665
  • 605
  • 1
  • 5
  • 6
59
votes
4 answers

How to get /etc/profile to run automatically in Alpine / Docker

How can I get /etc/profile to run automatically when starting an Alpine Docker container interactively? I have added some aliases to an aliases.sh file and placed it in /etc/profile.d, but when I start the container using docker run -it…
Jeff Kilbride
  • 2,614
  • 1
  • 20
  • 21
57
votes
13 answers

How can I tell if a file is older than 30 minutes from /bin/sh?

How do I write a script to determine if a file is older than 30 minutes in /bin/sh? Unfortunately does not the stat command exist in the system. It is an old Unix system, http://en.wikipedia.org/wiki/Interactive_Unix Perl is unfortunately not…
magol
  • 6,135
  • 17
  • 65
  • 120
55
votes
1 answer

Does bash have a way to un-export a variable without unsetting it?

Is it possible to export a variable in Bash, then later un-export it, without unsetting it entirely? I.e. have it still available to the current shell, but not to sub-processes. You can always do this, but it's ugly (and I'm curious): export…
Brendan
  • 1,995
  • 1
  • 20
  • 35
54
votes
3 answers

Equivalent of .bat in mac os

I currently use a .bat file that is utilized to invoke a java file. If I wanted to utilize the same functionality on Mac OS what format changes would I make? (unless the .bat equivalent on Mac OS is the .sh format?) java -cp …
Ganeshja
  • 2,675
  • 12
  • 36
  • 57
53
votes
2 answers

Running two commands sequentially in a cron job?

I have two commands in a cron job like this: mysql -xxxxxx -pyyyyyyyyyyv -hlocalhost -e "call MyFunction1";wget -N http://mywebsite.net/path/AfterMyFunction1.php but it seems to me that both of them are running at the same time. How can I make the…
medo ampir
  • 1,850
  • 7
  • 33
  • 57
53
votes
9 answers

Bash: evaluate a mathematical term?

echo 3+3 How can I evaluate such expressions in Bash, in this case to 6?
hhh
  • 50,788
  • 62
  • 179
  • 282
52
votes
6 answers

^word^replacement^ on all matches in Bash?

To clarify, I am looking for a way to perform a global search and replace on the previous command used. ^word^replacement^ only seems to replace the first match. Is there some set option that is eluding me?
pisswillis
  • 1,569
  • 2
  • 14
  • 19
51
votes
12 answers

How to read current app version in Xcode 11 with script

Until Xcode 11, I used a script that reads the current app version (for the AppStore) and help me change the LaunchScreen since we can't use swift for…
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
49
votes
4 answers

How to check if stdin is from the terminal or a pipe in a shell script?

I am writing a POSIX shell script that may or may not receive input from stdin as in foo.sh < test.txt, non-interactively. How do I check whether there is anything on stdin, to avoid halting on while read -r line...?
l0b0
  • 55,365
  • 30
  • 138
  • 223