Questions tagged [shellcheck]

Shellcheck refers to the ShellCheck program.

ShellCheck is a tool for linting shell scripts. It works in the browser or can be installed locally.

References

60 questions
25
votes
4 answers

Why does shellcheck fail when a source file is representing the variables at the top of a script?

Ubuntu 16.04 Bash 4.3.48 Why does shellcheck fail when a source file is representing the $variables at the top of a script? Here is a simple script: #!/bin/bash . .sourcefile echo "Today is ${day}." Here is my source file: …
Vituvo
  • 1,008
  • 1
  • 9
  • 29
21
votes
2 answers

How to suppress irrelevant ShellCheck messages?

Environment System: Linux Mint 19 (based on Ubuntu 18.04). Editor: I use Visual Studio Code (official website) with ShellCheck plugin to check for errors, warnings, and hints on-the-fly. ShellCheck is a necessary tool for every shell script…
Vlastimil Burián
  • 3,024
  • 2
  • 31
  • 52
18
votes
4 answers

How to be explicit about intentional word splitting?

I'm running shellcheck on my scripts and often get this warning (which in this case is correct, because cd foo bar baz makes no sense): cd ${SOME_DIR} || exit 1 ^-- SC2046: Quote this to prevent word splitting. This warning is mostly a good one.…
Andreas
  • 5,086
  • 3
  • 16
  • 36
18
votes
1 answer

What does this error mean? (SC2129: Consider using { cmd1; cmd2; } >> file instead of individual redirects.)

I'm writing a script to generate draft posts for my blog. After running ShellCheck, I keep seeing this error pop up. What does this mean and can someone provide an example? SC2129: Consider using { cmd1; cmd2; } >> file instead of individual…
Chris
  • 547
  • 6
  • 20
8
votes
1 answer

Best Practice : Print an array in a bash script

I ran shellcheck on my script and ran into an error on a very simple aspect - echo "List of fields deleted: ${deleted[@]}" ^-----------^ SC2145: Argument mixes string and array. Use * or separate argument. I am…
Poorva Jain
  • 83
  • 1
  • 4
7
votes
2 answers

ShellCheck warning: "Iterating over ls output is fragile. Use globs. [SC2045]"

I am getting a ShellCheck warning [SC2045] for the second line in the code below. Is it fine to ignore it as I am making sure the directory is not empty before I try the last ls? if [ "$(ls -A "$retryDir")" ] ; then for thisRetryFile in $(ls…
AndyM
  • 562
  • 4
  • 22
7
votes
1 answer

How to pass a glob to a shell script via an environment variable while passing ShellCheck

Let's say I have a shell script, test.sh, that is supposed to echo some number of values. I'd like to be able to pass those values in via an environment variable with glob expansion: $ ls 1.js 2.js test.sh* $ FOO="*.js" bash ./test.sh 1.js…
Domenic
  • 110,262
  • 41
  • 219
  • 271
6
votes
2 answers

Splitting a list in bash

I have this script: #!/bin/bash list="a b c d" for item in ${list[@]}; do echo "${item}" done When I run it this is the output: a b c d This is exactly what I want. However, shellcheck hates this and throws an error: for item in ${list[@]};…
Mausy5043
  • 906
  • 2
  • 17
  • 39
6
votes
1 answer

Why are && and || preferred to -a and -o

When writing if blocks in bash, shellcheck tells me that && and || are preferred to using -a and -o. Why? It is faster, or just simply a stylistic preference to make scripts look cleaner? The specific message I get is: ^-- SC2166: Prefer [ p ] || […
Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
6
votes
1 answer

Variable Interpolation vs. Substitution in printf Format String

ShellCheck gives a warning if you put a variable in the printf(1) format string. Why? Is: printf "$file does not exist\n" inferior in some way to: printf "%s does not exist\n" "$file"
rojomoke
  • 3,765
  • 2
  • 21
  • 30
5
votes
1 answer

How do you fix or mask a 1091 error found with shellcheck when including a source in a bash script?

Shellsheck is a static analysis tool for shell scripts which can be installed local on some Linux systems and can used without installation online for checking bash scripts for some…
user16468971
4
votes
1 answer

openBinaryFile: does not exist (No such file or directory) when using shellcheck

I am seeing this issue, on Ubuntu 18.04.4 LTS for shellcheck: $ ls -l /data/myscript -rwxr-xr-x 1 user 1300000013 4200 Aug 25 13:13 /data/myscript $ shellcheck /data/myscript /data/myscript: /data/myscript: openBinaryFile: does not exist (No such…
Roel Van de Paar
  • 2,111
  • 1
  • 24
  • 38
4
votes
2 answers

How can I remove the "SC2154" warning for environment variables

How can I remove shellcheck's warning "SC2154" when linting the shell script? #!/bin/bash set -euo pipefail IFS=$'\n\t' echo "proxy=$http_proxy" | sudo tee -a /etc/test.txt The warning is "SC2154: http_proxy is referenced but not assigned." EDIT:…
user5580578
  • 1,134
  • 1
  • 12
  • 28
4
votes
1 answer

Is it possible to resolve SC2001 ("See if you can use ${variable//search/replace} instead") while using a position variable?

I'm looking for a one liner to replace any character in a variable string at a variable position with a variable substitute. I came up with this working solution: echo "$string" | sed "s/./${replacement}/${position}" An example…
Alex Jansen
  • 1,455
  • 5
  • 18
  • 34
4
votes
1 answer

Extracting shell scripts from an RPM spec file for static analysis

I want to run ShellCheck on scripts embedded within a spec file that will run where the RPM is deployed. I have .spec snippets like, %setup -q cat > ./example.sh << EOF #!/bin/sh echo "example" EOF As well as hooks, %post #!/bin/sh echo…
artless noise
  • 21,212
  • 6
  • 68
  • 105
1
2 3 4