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
391
votes
21 answers

Check if a file exists with a wildcard in a shell script

I'm trying to check if a file exists, but with a wildcard. Here is my example: if [ -f "xorg-x11-fonts*" ]; then printf "BLAH" fi I have also tried it without the double quotes.
Danny
  • 5,180
  • 6
  • 26
  • 29
368
votes
8 answers

What does $@ mean in a shell script?

What does a dollar sign followed by an at-sign (@) mean in a shell script? For example: umbrella_corp_options $@
trusktr
  • 44,284
  • 53
  • 191
  • 263
321
votes
6 answers

What is the difference between $(command) and `command` in shell programming?

To store the output of a command as a variable in sh/ksh/bash, you can do either var=$(command) or var=`command` What's the difference if any between the two methods?
hhafez
  • 38,949
  • 39
  • 113
  • 143
315
votes
8 answers

Select unique or distinct values from a list in UNIX shell script

I have a ksh script that returns a long list of values, newline separated, and I want to see only the unique/distinct values. It is possible to do this? For example, say my output is file suffixes in a…
brabster
  • 42,504
  • 27
  • 146
  • 186
285
votes
13 answers

How do I run a shell script without using "sh" or "bash" commands?

I have a shell script which I want to run without using the "sh" or "bash" commands. For example: Instead of: sh script.sh I want to use: script.sh How can I do this? P.S. (i) I don't use shell script much and I tried reading about aliases, but I…
Rameez Hussain
  • 6,414
  • 10
  • 56
  • 85
280
votes
14 answers

source command not found in sh shell

I have a script that uses sh shell. I get an error in the line that uses the source command. It seems source is not included in my sh shell. If I explicitly try to run source from shell I get: sh: 1: source: not found Should I somehow install…
Milad
  • 4,901
  • 5
  • 32
  • 43
277
votes
6 answers

How to redirect output of an entire shell script within the script itself?

Is it possible to redirect all of the output of a Bourne shell script to somewhere, but with shell commands inside the script itself? Redirecting the output of a single command is easy, but I want something more like this: #!/bin/sh if [ ! -t 0 ];…
Steve Madsen
  • 13,465
  • 4
  • 49
  • 67
275
votes
8 answers

A variable modified inside a while loop is not remembered

In the following program, if I set the variable $foo to the value 1 inside the first if statement, it works in the sense that its value is remembered after the if statement. However, when I set the same variable to the value 2 inside an if which is…
Eric Lilja
  • 3,323
  • 4
  • 18
  • 15
268
votes
16 answers

How can I remove the extension of a filename in a shell script?

What's wrong with the following code? name='$filename | cut -f1 -d'.'' As is, I get the literal string $filename | cut -f1 -d'.', but if I remove the quotes I don't get anything. Meanwhile, typing "test.exe" | cut -f1 -d'.' in a shell gives me the…
mimicocotopus
  • 5,280
  • 4
  • 22
  • 24
263
votes
3 answers

Using variables inside a bash heredoc

I'm trying to interpolate variables inside of a bash heredoc: var=$1 sudo tee "/path/to/outfile" > /dev/null << "EOF" Some text that contains my $var EOF This isn't working as I'd expect ($var is treated literally, not expanded). I need to use sudo…
Jon
  • 3,280
  • 2
  • 15
  • 16
260
votes
7 answers

Compare a string using sh shell

I am using SH shell and I am trying to compare a string with a variable's value but the if condition is always execute to true. Why? Here is some code: Sourcesystem="ABC" if [ "$Sourcesystem" -eq 'XYZ' ]; then echo "Sourcesystem Matched"…
James Bond
  • 2,825
  • 2
  • 15
  • 11
256
votes
4 answers

Calling shell functions with xargs

I am trying to use xargs to call a more complex function in parallel. #!/bin/bash echo_var(){ echo $1 return 0 } seq -f "n%04g" 1 100 |xargs -n 1 -P 10 -i echo_var {} exit 0 This returns the error xargs: echo_var: No such file or…
fac3
  • 2,681
  • 2
  • 14
  • 5
251
votes
6 answers

How do I limit the number of results returned from grep?

I would like to say 10 lines max from grep. I don't want my computer to work hard. I want it to stop after 10 results found by grep. Is it possible?
Jas
  • 14,493
  • 27
  • 97
  • 148
248
votes
9 answers

What is the benefit of using $() instead of backticks in shell scripts?

There are two ways to capture the output of command line in bash: Legacy Bourne shell backticks ``: var=`command` $() syntax (which as far as I know is Bash specific, or at least not supported by non-POSIX old shells like original…
DVK
  • 126,886
  • 32
  • 213
  • 327
245
votes
12 answers

Git Alias - Multiple Commands and Parameters

I am trying to create an alias that uses both multiple Git commands and positional parameters. There are Stackoverflow pages for each, and it would appear painfully obvious to do both, but I am having trouble. As an example, I want to switch to…
Stella
  • 2,453
  • 2
  • 14
  • 5