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
122
votes
8 answers

Terminal error: zsh: permission denied: ./startup.sh

I am running a command ./startup.sh nginx:start and I am getting this error message zsh: permission denied: ./startup.sh why could this be happening?
Reacting
  • 5,543
  • 7
  • 35
  • 86
110
votes
3 answers

Forward function declarations in a Bash or a Shell script?

Is there such a thing in bash or at least something similar (work-around) like forward declarations, well known in C / C++, for instance? Or there is so such thing because for example it is always executed in one pass (line after line)? If there are…
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
109
votes
9 answers

How can I show the wget progress bar only?

For example: wget http://somesite.com/TheFile.jpeg downloading: TheFile.tar.gz ... --09:30:42-- http://somesite.com/TheFile.jpeg => `/home/me/Downloads/TheFile.jpeg' Resolving somesite.co... xxx.xxx.xxx.xxx. …
Flan Alflani
  • 2,065
  • 3
  • 15
  • 17
108
votes
6 answers

envsubst: command not found on Mac OS X 10.8

When I try to run a script that contains the envsubst command, I get this error. Looking online, this seems to be a standard bash command, so I am not sure what to install in order to get it to work.
Andrew
  • 6,295
  • 11
  • 56
  • 95
107
votes
7 answers

Shell script current directory?

What is current directory of shell script? Is this current directory from which I called it? Or this directory where the script is located?
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
103
votes
8 answers

shopt command not found in .bashrc after shell updation

I have updated my shell to ZSH. When I source ~/.bashrc. I am getting this error There was some error in yo doctor . when i execute this command echo "export NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules" >> ~/.bashrc && source…
Amerrnath
  • 2,227
  • 4
  • 22
  • 33
101
votes
16 answers

Are shell scripts sensitive to encoding and line endings?

I am making an NW.js app on macOS, and want to run the app in dev mode by double-clicking on an icon. In the first step, I'm trying to make my shell script work. Using VS Code on Windows (I wanted to gain time), I have created a run-nw file at the…
thomasb
  • 5,816
  • 10
  • 57
  • 92
94
votes
8 answers

How do I merge one directory into another using Bash?

I'm looking for shell script that merge files from one directory into another. Sample: html/ a/ b.html index.html html_new/ a/ b2.html b.html Usage: ./mergedirs.sh html html_new Result: html/ a/ b.html b2.html …
NVI
  • 14,907
  • 16
  • 65
  • 104
92
votes
3 answers

How to get script directory in POSIX sh?

I have the following code in my bash script. Now I wanna use it in POSIX sh. How can I convert it? DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" > /dev/null && pwd )"
city
  • 2,036
  • 2
  • 32
  • 40
92
votes
8 answers

Randomly shuffling lines in Linux / Bash

I have some files in linux. For example 2 and i need shuffling the files in one file. For example $cat file1 line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 and $cat file2 linea one linea two linea three linea four linea five linea…
Code Geas Coder
  • 1,839
  • 4
  • 23
  • 29
89
votes
2 answers

Replacement for source in sh

I need to set the environment variables, usually we do this by source script.sh But now, I am automating it during the boot process and it looks like the root boots by default with sh shell. How do I source this script in sh?
vkris
  • 2,095
  • 7
  • 22
  • 30
89
votes
1 answer

What exactly is sudo bang bang?

What is the purpose of sudo bang bang (sudo !!) and when might one need to use it?
alestanis
  • 21,519
  • 4
  • 48
  • 67
86
votes
2 answers

Shell Script: How to write a string to file and to stdout on console?

How to write a string to file and to stdout on console? If I do echo "hello" > logfile.txt I view only hello in logfile.txt but how can I write hello also on console of Linux?
Catanzaro
  • 1,312
  • 2
  • 13
  • 24
85
votes
8 answers

How to get shell to self-detect using zsh or bash

I've a question on how to tell which shell the user is using. Suppose a script that if the user is using zsh, then put PATH to his .zshrc and if using bash should put in .bashrc. And set rvmrc accordingly. #!/usr/bin/env bash export…
Juanito Fatas
  • 9,419
  • 9
  • 46
  • 70
84
votes
12 answers

Shell Script Syntax Error: Unexpected End of File

In the following script I get an error: syntax error: unexpected end of file What is this error how can I resove it? It is pointing at the line whee the function is…
Rajeev
  • 44,985
  • 76
  • 186
  • 285