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
244
votes
4 answers

Piping command output to tee but also save exit code of command

I have a shell script in which I wrap a command (mvn clean install), to redirect the output to a logfile. #!/bin/bash ... mvn clean install $@ | tee $logfile echo $? # Does not show the return code of mvn clean install Now if mvn clean install…
Wolkenarchitekt
  • 20,170
  • 29
  • 111
  • 174
214
votes
11 answers

How to run .sh on Windows Command Prompt?

How can I run .sh on Windows 7 Command Prompt? I always get this error when I try to run this line in it, app/build/build.sh error, 'app' is not recognized... or, bash app/build/build.sh error, 'bash' is not recognized... Any ideas what have I…
Run
  • 54,938
  • 169
  • 450
  • 748
191
votes
7 answers

How to cat <> a file containing code?

I want to print code into a file using cat <>: cat <> brightup.sh !/bin/bash curr=`cat /sys/class/backlight/intel_backlight/actual_brightness` if [ $curr -lt 4477 ]; then curr=$((curr+406)); echo $curr >…
UberNate
  • 2,109
  • 2
  • 15
  • 15
175
votes
5 answers

What does the line "#!/bin/sh" mean in a UNIX shell script?

I was going through some shell script tutorials and found the following sample program: #!/bin/sh clear echo "HELLO WORLD" Can anyone please tell me what the significance of the comment #!/bin/sh at the start is?
Jake
  • 16,329
  • 50
  • 126
  • 202
169
votes
10 answers

Count occurrences of a char in a string using Bash

I need to count the number of occurrences of a char in a string using Bash. In the following example, when the char is (for example) t, it echos the correct number of occurrences of t in var, but when the character is comma or semicolon, it prints…
Jericob
  • 1,947
  • 2
  • 14
  • 16
168
votes
11 answers

Deleting lines from one file which are in another file

I have a file f1: line1 line2 line3 line4 .. .. I want to delete all the lines which are in another file f2: line2 line8 .. .. I tried something with cat and sed, which wasn't even close to what I intended. How can I do this?
lalli
  • 6,083
  • 7
  • 42
  • 55
160
votes
2 answers

What does it mean in shell when we put a command inside dollar sign and parentheses: $(command)

I just want to understand following line of code in shell. It is used to get the current working directory. I am aware that $(variable) name return the value inside the variable name, but what is $(command) supposed to return? Does it return the…
KItis
  • 5,476
  • 19
  • 64
  • 112
156
votes
4 answers

Multiline syntax for piping a heredoc; is this portable?

I'm familiar with this syntax: cmd1 << EOF | cmd2 text EOF but just discovered that bash allows me to write: cmd1 << EOF | text EOF cmd2 (the heredoc is used as input to cmd1, and the output of cmd1 is piped to cmd2). This seems like a very odd…
William Pursell
  • 204,365
  • 48
  • 270
  • 300
152
votes
7 answers

I just assigned a variable, but echo $variable shows something else

Here are a series of cases where echo $var can show a different value than what was just assigned. This happens regardless of whether the assigned value was "double quoted", 'single quoted' or unquoted. How do I get the shell to set my variable…
that other guy
  • 116,971
  • 11
  • 170
  • 194
151
votes
15 answers

How can I extract the first two characters of a string in shell scripting?

For example, given: USCAGoleta9311734.5021-120.1287855805 I want to extract just: US
Greg
  • 2,221
  • 4
  • 19
  • 11
151
votes
18 answers

Remove redundant paths from $PATH variable

I have defined the same path in the $PATH variable 6 times. I wasn't logging out to check whether it worked. How can I remove the duplicates? The $PATH variable looks like this: echo…
charles hendry
  • 1,710
  • 4
  • 13
  • 18
143
votes
13 answers

Get specific line from text file using just shell script

I am trying to get a specific line from a text file. So far, online I have only seen stuff like sed, (I can only use the sh -not bash or sed or anything like that). I need to do this only using a basic shell script. cat file | while read line …
GangstaGraham
  • 8,865
  • 12
  • 42
  • 60
140
votes
13 answers

Cron jobs and random times, within given hours

I need the ability to run a PHP script 20 times a day at completely random times. I also want it to run only between 9am - 11pm. I'm familiar with creating cron jobs in linux.
floatleft
  • 6,243
  • 12
  • 43
  • 53
132
votes
7 answers

What's a .sh file?

So I am not experienced in dealing with a plethora of file types, and I haven't been able to find much info on exactly what .sh files are. Here's what I'm trying to do: I'm trying to download map data sets which are arranged in tiles that can be…
Tony H
  • 1,339
  • 2
  • 9
  • 4
123
votes
6 answers

Recursively change file extensions in Bash

I want to recursively iterate through a directory and change the extension of all files of a certain extension, say .t1 to .t2. What is the bash command for doing this?
Amal Antony
  • 6,477
  • 14
  • 53
  • 76