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
82
votes
5 answers

echo >&2 "some text" what does it mean in shell scripting

I have seen echo being used like this in many places: echo >&2 message text ... What does this mean? I understand 2>&1, however, I am not sure how to interpret the usage above. Can anyone please explain?
CuriousMind
  • 8,301
  • 22
  • 65
  • 134
79
votes
2 answers

What is /bin/sh -c?

What does /bin/sh -c mean? What does -c do?
user481831
  • 953
  • 2
  • 7
  • 8
77
votes
3 answers

move only if file exists in a shell script

I want to use mv to rename a file: mv src.txt dest.txt If the file doesn't exist, I get an error: mv: cannot stat ‘src.txt’: No such file or directory How do I use mv only if the file already exists? I don't want to redirect stderr to dev/null as…
Arth
  • 12,789
  • 5
  • 37
  • 69
77
votes
5 answers

How to comment out particular lines in a shell script

Can anyone suggest how to comment particular lines in the shell script other than #? Suppose I want to comment five lines. Instead of adding # to each line, is there any other way to comment the five lines?
user2400564
  • 4,619
  • 7
  • 24
  • 27
76
votes
4 answers

How does Ctrl-C terminate a child process?

I am trying to understand how CTRL+C terminates a child but not a parent process. I see this behavior in some script shells like bash where you can start some long-running process and then terminate it by entering CTRL-C and the control returns to…
vitaut
  • 49,672
  • 25
  • 199
  • 336
75
votes
2 answers

What's the meaning of a ! before a command in the shell?

What is the purpose of a shell command (part of a shell script) starting with an exclamation mark? Concrete example: In foo.sh: #!/usr/bin/env bash set -e ! docker stop foo ! docker rm -f foo # ... other stuff I know that without the space the…
sthzg
  • 5,514
  • 2
  • 29
  • 50
74
votes
6 answers

TERM environment variable not set

I have a file.sh with this, when run show : TERM environment variable not set. smbmount //172.16.44.9/APPS/Interfas/HERRAM/sc5 /mnt/siscont5 -o iocharset=utf8,username=backup,password=backup2011,r if [ -f /mnt/siscont5/HER.TXT ]; then echo "No…
meyquel
  • 2,134
  • 5
  • 23
  • 42
72
votes
15 answers

ZSH not recognizing my aliases?

Using iTerm2 with zsh and it isn't recognizing my aliases. Sometimes I have to work in an IDE and can't just easily vim something and the stupid people thought it a good idea to name their applications like MyReallyLongApplicationName.app and since…
o_O
  • 5,527
  • 12
  • 52
  • 90
71
votes
11 answers

How to copy and edit files in Android shell?

The Android shell does not have the cp command. Android shell also has no sed or grep or vi. I have no adb daemon available. There is mv command but it rejects to work if source is on a read-only device. What to do if I have to copy some…
psihodelia
  • 29,566
  • 35
  • 108
  • 157
69
votes
4 answers

Why does /bin/sh behave differently to /bin/bash even if one points to the other?

While I was playing around in my shell investigating the answer to this question, I noticed that, even though /bin/sh was pointing to /bin/bash on my system, the two commands behave differently. First of all, the output of ls -lh…
Squirrel
  • 2,262
  • 2
  • 18
  • 29
68
votes
5 answers

Inline if shell script

Is it possible to execute shell script in command line like this : counter=`ps -ef | grep -c "myApplication"`; if [ $counter -eq 1 ] then; echo "true"; > Above example is not working I get only > character not the result I'm trying to get, that is…
London
  • 14,986
  • 35
  • 106
  • 147
68
votes
3 answers

Pipe output to environment variable export command

I'm trying to set a git hash value into an environment variable, i thought it would be as simple as doing this: git log --oneline -1 | export GIT_HASH=$1 But the $1 doesn't contain anything. What am I doing wrong?
Mark Unsworth
  • 3,027
  • 2
  • 20
  • 21
68
votes
3 answers

Shell: redirect stdout to /dev/null and stderr to stdout

I saw this interesting question at a comment on cyberciti.biz. That I found I even can't find a flexible way to do this in one-line command with sh. As far my thought for the solution is: tmp_file=`mktemp` (./script 2>$tmp_file >/dev/null; cat…
shouya
  • 2,863
  • 1
  • 24
  • 45
67
votes
8 answers

How to calculate the log of a number using bc?

I want to calculate the log (base 10) of a number. How can I do this using bc?
Bruce
  • 33,927
  • 76
  • 174
  • 262
67
votes
6 answers

Dockerfile CMD instruction will exit the container just after running it

I want to setup some configuration when my container starts, for this I am using shell scripts. But my container will exits as soon as my scripts ends, I have tried with -d flag / detached mode but It will never run in detached mode. Below is my…
Anand Suthar
  • 3,678
  • 2
  • 30
  • 52