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
2
votes
2 answers

different result for [[ ]] and [] string comparator "<"

I was doing some tests, and I couldn't understand why there two results are different. The first one seems correct, since in a crescent ordenation, the character 'f' comes first than 'F' $ [[ "Foo" < "foo" ]] $ echo $? 1 So why this one is…
2
votes
4 answers

Concatenating digits from a string in sh

Assuming that I have a string like this one: string="1 0 . @ 1 1 ? 2 2 4" Is it possible to concatenate digits that are next to each other? So that string be like: 10 . @ 11 ? 224 ? I found only basic things how to distinguish integers from other…
davoid
  • 327
  • 2
  • 10
2
votes
4 answers

Strip characters in bash output

I have this command which is working for me, which will find zero or one directories in the current directory which match a pattern: find . -maxdepth 1 -type d -name 'suman-*'| head -n1 on MacOS, this will result in something…
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
2
votes
1 answer

How to make an executable shell script macOS that can be used on any computer

I have a shell file, and I need to make an executable that will work on any Mac without chmod +x, just by double clicking it. How can I do this? I've tried appify, but double clicking its result does not execute my file. I have also tried the…
Aditya J.
  • 131
  • 2
  • 11
2
votes
3 answers

How to extract strings from a text in shell

I have a file name "PHOTOS_TIMESTAMP_5373382" I want to extract from this filename "PHOTOS_5373382" and add "ABC" i.e. finally want it to look like "abc_PHOTOS_5373382" in shell script.
divya.trehan573
  • 454
  • 1
  • 12
  • 28
2
votes
2 answers

Pass arguments to mongo script

I am executing a mongo script in command line with following command. mongo --quiet --eval remove_audits.js I want to pass arguments to mongo script like below. mongo --quiet --eval remove_audits.js arg1 arg2 Let me know how can I do this for mongo…
2
votes
1 answer

escape a whole array of arguments for use in sh-like shells with a standard tool

I'm looking for a standard tool capable of taking all of its arguments and turning it into a single string suitable for use as multiple arguments in an automatically generated bash/sh/zsh script. Such a command is extremely useful in various…
enigmaticPhysicist
  • 1,518
  • 16
  • 21
2
votes
5 answers

How to grep and remove from a file all lines between a separator

I have a file that looks like this: ===SEPARATOR=== line2 line3 ===SEPARATOR=== line5 line6 ===SEPARATOR=== line8 ... lineX ===SEPARATOR=== How can I do a while loop and go through the file, dump anything between two ===SEPARATOR=== occurrences…
Nick Constantine
  • 963
  • 9
  • 19
2
votes
5 answers

Optimizing bash script (for loops, permissions, ...)

I made this script (very quickly ... :)) ages ago and use it very often, but now I'm interested how bash experts would optimize it. What it does is it goes through files and directories in the current directory and sets the correct…
zigamilek
  • 301
  • 1
  • 3
  • 11
2
votes
2 answers

Is it possible to several commands in the background but wait for all results and fail the script if an command fails

I have a CI script that I want to speed up by running several things in the background. I want the script wait for all processes and check each one to see if it failed. Here a a simplification: #!/bin/bash set -e bg() { sleep .$[ ( $RANDOM %…
Robert
  • 37,670
  • 37
  • 171
  • 213
2
votes
1 answer

Fast Keyevent Simulation (Android Shell)

Using adb shell input text or adb shell input keyevent works perfectly fine in sending text to the android device, but my issue is speed. Using something like input keyevent KEYCODE_A KEYCODE_A KEYCODE_SPACE KEYCODE_A…
SamJ
  • 413
  • 1
  • 4
  • 18
2
votes
0 answers

debian service vs /etc/init.d/script

I'm trying to install tomcat using [these]https://www.mkyong.com/tomcat/how-to-install-apache-tomcat-8-on-debian/ instructions. Accordingly, I create a shell script inside the /etc/init.d directory: #!/bin/sh export CATALINA_HOME=/opt/tomcat9 export…
Conor
  • 535
  • 2
  • 8
  • 16
2
votes
0 answers

AIX shell execution permission denied even with sudo

I'm trying to execute a shell script on a AIX server, using bash, where I have speruser rights and I'm still getting this error. Is there a way to bypass the "permission denied" problem? /files/data/ant/apache-ant-1.8.2/bin/ant:…
SHPstr
  • 91
  • 9
2
votes
1 answer

Disable sudo for a single command in a sh file on macOS Sierra

I have been looking everywhere for something to disable sudo access for this one command while maintaining sudo for the rest for the script. The script is called using sudo e.g. "sudo ./install.sh". The snippet in question is this (I currently was…
user3338275
  • 240
  • 2
  • 3
  • 13
2
votes
3 answers

Getting values in a file which is not present in another file

I have two files File1.txt: docker/registry:2.4.2 docker/rethinkdb:latest docker/swarm:1.0.0 File2.txt: docker/registry:2.4.1 docker/rethinkdb:1.0.0 docker/swarm:1.0.0 The output should be: docker/registry:2.4.2 docker/rethinkdb:latest In other…
meallhour
  • 13,921
  • 21
  • 60
  • 117