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.

7327 questions
2911
votes
26 answers

How to check if a string contains a substring in Bash

I have a string in Bash: string="My string" How can I test if it contains another string? if [ $string ?? 'foo' ]; then echo "It's there!" fi Where ?? is my unknown operator. Do I use echo and grep? if echo "$string" | grep 'foo'; then echo…
davidsheldon
  • 32,393
  • 4
  • 25
  • 27
1521
votes
12 answers

Difference between sh and bash

When writing shell programs, we often use /bin/sh and /bin/bash. I usually use bash, but I don't know what's the difference between them. What's main difference between bash and sh? What do we need to be aware of when programming in bash and sh?
Weiwei Yang
  • 15,361
  • 3
  • 13
  • 10
1144
votes
23 answers

How can I declare and use Boolean variables in a shell script?

I tried to declare a Boolean variable in a shell script using the following syntax: variable=$false variable=$true Is this correct? Also, if I wanted to update that variable would I use the same syntax? Finally, is the following syntax for using…
hassaanm
  • 11,789
  • 4
  • 18
  • 20
1041
votes
14 answers

How to echo shell commands as they are executed

In a shell script, how do I echo all shell commands called and expand any variable names? For example, given the following line: ls $DIRNAME I would like the script to run the command and display the following ls /full/path/to/some/dir The purpose…
Jack Nock
  • 12,489
  • 4
  • 20
  • 18
856
votes
8 answers

What does set -e mean in a bash script?

I'm studying the content of this preinst file that the script executes before that package is unpacked from its Debian archive (.deb) file. The script has the following code: #!/bin/bash set -e # Automatically added by dh_installinit if [ "$1" =…
AndreaNobili
  • 34,200
  • 85
  • 240
  • 456
608
votes
9 answers

"echo -n" prints "-n"

I have a problem with echo in my script: echo -n "Some string..." prints -n Some string... and moves to the next line. In the console it's working correcly without newline: Some string...
qwertz
  • 13,850
  • 9
  • 31
  • 45
588
votes
8 answers

How to do a logical OR operation for integer comparison in shell scripting?

I am trying to do a simple condition check, but it doesn't seem to work. If $# is equal to 0 or is greater than 1 then say hello. I have tried the following syntax with no success: if [ "$#" == 0 -o "$#" > 1 ] ; then echo "hello" fi if [ "$#" == 0…
Strawberry
  • 58,930
  • 53
  • 138
  • 190
588
votes
5 answers

Command not found error in Bash variable assignment

I have this script called test.sh: #!/bin/bash STR = "Hello World" echo $STR when I run sh test.sh I get this: test.sh: line 2: STR: command not found What am I doing wrong? I look at extremely basic/beginners bash scripting tutorials online and…
Jake Wilson
  • 78,902
  • 83
  • 230
  • 344
579
votes
7 answers

How to read a file into a variable in shell?

I want to read a file and save it in variable, but I need to keep the variable and not just print out the file. How can I do this? I have written this script but it isn't quite what I needed: #!/bin/sh while read LINE do echo $LINE done <$1 …
kaka
  • 5,995
  • 4
  • 16
  • 22
488
votes
10 answers

How to get a password from a shell script without echoing

I have a script that automates a process that needs access to a password protected system. The system is accessed via a command-line program that accepts the user password as an argument. I would like to prompt the user to type in their password,…
BD at Rivenhill
  • 10,715
  • 9
  • 42
  • 49
433
votes
2 answers

How can I split a shell command over multiple lines when using an IF statement?

How can I split a command over multiple lines in the shell, when the command is part of an if statement? This works: if ! fab --fabfile=.deploy/fabfile.py --forward-agent --disable-known-hosts deploy:$target; then rc=1 …
Dmitry Minkovsky
  • 30,036
  • 20
  • 97
  • 138
429
votes
21 answers

How to execute mongo commands through shell scripts?

I want to execute mongo commands in shell script, e.g. in a script test.sh: #!/bin/sh mongo myDbName db.mycollection.findOne() show collections When I execute this script via ./test.sh, then the connection to MongoDB is established, but the…
StackOverFlow
  • 4,089
  • 9
  • 45
  • 82
401
votes
25 answers

How can I get the behavior of GNU's readlink -f on a Mac?

On Linux, the readlink utility accepts an option -f that follows additional links. This doesn't seem to work on Mac and possibly BSD based systems. What would the equivalent be? Here's some debug information: $ which readlink; readlink…
troelskn
  • 107,146
  • 23
  • 127
  • 148
380
votes
13 answers

How can I have a newline in a string in sh?

This STR="Hello\nWorld" echo $STR produces as output Hello\nWorld instead of Hello World What should I do to have a newline in a string? Note: This question is not about echo. I'm aware of echo -e, but I'm looking for a solution that allows…
Juan A. Navarro
  • 8,897
  • 6
  • 42
  • 49
363
votes
21 answers

How to run a cron job inside a docker container?

I am trying to run a cronjob inside a docker container that invokes a shell script. Yesterday I have been searching all over the web and stack overflow, but I could not really find a solution that works. How can I do this? EDIT: I've created a…
C Heyer
  • 3,633
  • 3
  • 8
  • 6
1
2 3
99 100