Questions tagged [shellcheck]

shellcheck is a tool for automatically detecting shell script problems

ShellCheck is a static analysis and linting tool for sh/bash/ksh/zsh scripts.

It's mainly focused on handling typical beginner and intermediate level syntax errors and pitfalls where the shell just gives a cryptic error message or strange behavior, but it also reports on a few more advanced issues where corner cases can cause delayed failures.

23 questions
17
votes
1 answer

Bash: What does "masking return values" mean?

shellcheck generated the following warning SC2155: Declare and assign separately to avoid masking return values For this line of code local key_value=$(echo "$current_line" | mawk '/.+=.+/ {print $1 }') What does "masking return values" mean, and…
user332459
9
votes
2 answers

ShellCheck is carping that my expression is not in double quotes when it really is; why?

I'm writing a bash script with the AWS CLI and shellcheck is giving an error that I think is incorrect. I'd like to try to figure out why its carping. Here's the code, and error message: for server in $(${aws} ec2 describe-instances --query…
Michael J
  • 573
  • 4
  • 9
7
votes
1 answer

Unexpected outcome of a="$@"

I'm struggling with this situation: $ set -- 1 2 3 $ a="$@" $ echo "$a" 1 2 3 What I find unexpected is the assignment itself. man bash says this about the "$@" expansion: When the expansion occurs within double quotes, each parameter expands to…
user147505
7
votes
1 answer

bash shellcheck issue with variables in brace expansion

I'm trying to create an array of file names, based on two variable and using brace expansion, like this: #!/bin/bash altdir=/usr arg=abc tries=({.,$altdir}/{$arg,$arg/main}.{tex,ltx,drv,dtx}) for i in "${tries[@]}"; do echo $i; done The last…
Wybo Dekker
  • 173
  • 1
  • 5
6
votes
1 answer

VS Code editor | How to include -x switch for ShellCheck?

Problem description and reproduction In terminal run: shellcheck -x my_script Where my_script sources another (partial) script. I get no error with the -x switch, but if I run it without -x: shellcheck my_script I get notification like this one: .…
Vlastimil Burián
  • 27,586
  • 56
  • 179
  • 309
5
votes
4 answers

Shellcheck approved way to pass 2 arguments in one variable, to a command in a bash script

In a bash script, I call another programme, but I want to configure that programme with a command line option. The following works: AREA_ARG="" if __SOME_SETTING__ ; then AREA_ARG=" --area us,ca " fi process_data -i /some/path $AREA_ARG i.e.…
Amandasaurus
  • 1,186
  • 2
  • 12
  • 21
5
votes
1 answer

if ! (...) vs. ; if [ $? -eq 0 ] (...)

I am working on a shell script and decided to check my work via shellcheck.net. I am able to get functionally the same behavior of the following two lines in my script: findmnt /dev/sda1 >/dev/null ; if [ $? -eq 0 ]; then echo 1; else echo 0;…
Kahn
  • 1,652
  • 2
  • 19
  • 36
4
votes
1 answer

how to get user input with a prompt into a variable in a posix compliant way

read -r -p "put an option: " option echo $option this works but shellcheck gives me: In POSIX sh, read -p is undefined. How to get user input with a prompt into a variable in a posix compliant way?
testoflow
  • 117
  • 8
4
votes
2 answers

ShellCheck warning regarding quoting ("A"B"C")

I am writing simple shell script and when I check my script at https://www.shellcheck.net it give me error at line 14 Line 14: sysrc ifconfig_"${Bridge}"="addm ${NIC}" ^-- SC2140: Word is of the form "A"B"C" (B…
Amr
  • 61
  • 4
4
votes
3 answers

What is the difference between double-quoting and not double-quoting an array in Bash?

While tracking down an error in my shellscript, I found the following behavior in this code snippet: declare -a filelist readarray filelist < <(ls -A) readonly filelist for file in "${filelist[@]}"; do sha256sum ${filelist[$file]} | head -c…
dcwaters
  • 451
  • 3
  • 13
4
votes
2 answers

Why must I not quote a string variable when running it as a command?

My Ubuntu/Debian-based Linux update POSIX shell script seems to require that I not double quote the string variable with the stored command, which is being executed. As I don't understand this issue, I'd like to ask why that is? And if the code is…
Vlastimil Burián
  • 27,586
  • 56
  • 179
  • 309
4
votes
2 answers

shellcheck warns about loops over find output even when given the path to begin search

Ubuntu 16.04 #!/bin/bash site="hello" wDir="/home/websites/${site}/httpdocs/" for file in $(find "${wDir}" -name "*.css") do echo "$file"; done exit 0; shellcheck warns me even if I define the start directory but the script works just…
Vituvo
  • 401
  • 7
  • 17
3
votes
1 answer

Unexpected EOF while looking for matching `"' problem

I need some help figuring out where my code is hitching up. The code is below: servers=( Sanger ) races=( American African Asian) jobbs=( NCBI ) ranges=( 1-2, 2-3, 3-4 ) for server in "${servers[@])" do for job in "${jobbs[@]}" do for…
Jared
  • 33
  • 1
  • 1
  • 3
3
votes
2 answers

Shellcheck complains that I should not to read and write the same file in the same pipeline

ShellCheck show the following error for this line of code: printf '%d' $(($(< "$1") + 1)) > "$1" Make sure not to read and write the same file in the same pipeline Is this really a problem? Could reading and writing the same file result in a race…
helpermethod
  • 1,952
  • 2
  • 19
  • 25
1
vote
1 answer

Why do I need to disable=SC2031

Can you tell me, why I have to # shellcheck disable=SC2030 in the following script? get_names_and_hosts(){ unset LOCAL_HOSTS declare -a LOCAL_HOSTS unset LOCAL_NAMES declare -a LOCAL_NAMES multipass list --format json | jq -r '.list[] |…
Chris G.
  • 143
  • 5
1
2