Skip to main content
edited title
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 267

I am getting a syntax"syntax error near unexpected token `'$#''" in a simple Bash script

Copy edited (e.g. ref. <http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29> and <https://en.wikipedia.org/wiki/Debian_version_history#Debian_10_(Buster)>).
Source Link

I am getting a syntax error in a simple bashBash script

I'm using Debian 10Debian 10 (Buster) and am getting an error in a simple bashBash script. All it does is check to see if 1one parameter is passed in, and if it's a file, just echo out the file:

#!/usr/bin/bash
if [ ( '$#' -eq 1 ) && ( -f "$1" ) ]; then
        echo "$1"
fi

exit 1

I get this error:

line 2: syntax error near unexpected token `'$#''
line 2: `if [ ( '$#' -eq 1 ) && ( -f "$1" ) ]; then'

I have tried every combination of quotes (", ', no quotes) around the $#$#, and I always get a variant of those error messages using the type of quotes I use. I can't figure out what it's looking for.

getting syntax error in simple bash script

I'm using Debian 10 and getting an error in a simple bash script. All it does is check to see if 1 parameter is passed in, and if it's a file just echo out the file:

#!/usr/bin/bash
if [ ( '$#' -eq 1 ) && ( -f "$1" ) ]; then
        echo "$1"
fi

exit 1

I get this error:

line 2: syntax error near unexpected token `'$#''
line 2: `if [ ( '$#' -eq 1 ) && ( -f "$1" ) ]; then'

I have tried every combination of quotes (", ', no quotes) around the $# and I always get a variant of those error messages using the type of quotes I use. I can't figure out what it's looking for.

I am getting a syntax error in a simple Bash script

I'm using Debian 10 (Buster) and am getting an error in a simple Bash script. All it does is check to see if one parameter is passed in, and if it's a file, just echo out the file:

#!/usr/bin/bash
if [ ( '$#' -eq 1 ) && ( -f "$1" ) ]; then
        echo "$1"
fi

exit 1

I get this error:

line 2: syntax error near unexpected token `'$#''
line 2: `if [ ( '$#' -eq 1 ) && ( -f "$1" ) ]; then'

I have tried every combination of quotes (", ', no quotes) around the $#, and I always get a variant of those error messages using the type of quotes I use. I can't figure out what it's looking for.

Tweeted twitter.com/StackUnix/status/1172389591535407108
Became Hot Network Question
added 2 characters in body
Source Link
muru
  • 78.4k
  • 16
  • 214
  • 320

I'm using Debian 10 and getting an error in a simple bash script. All it does is check to see if 1 parameter is passed in, and if it's a file just echo out the file:

#!/usr/bin/bash
if [ ( '$#' -eq 1 ) && ( -f "$1" ) ]; then
        echo "$1"
fi

exit 1

I get this error: line 2: syntax error near unexpected token '$#'' line 2: if [ ( '$#' -eq 1 ) && ( -f "$1" ) ]; then'

line 2: syntax error near unexpected token `'$#''
line 2: `if [ ( '$#' -eq 1 ) && ( -f "$1" ) ]; then'

I have tried every combination of quotes (", ', no quotes) around the $# and I always get a variant of those error messages using the type of quotes I use. I can't figure out what it's looking for.

I'm using Debian 10 and getting an error in a simple bash script. All it does is check to see if 1 parameter is passed in, and if it's a file just echo out the file:

#!/usr/bin/bash
if [ ( '$#' -eq 1 ) && ( -f "$1" ) ]; then
        echo "$1"
fi

exit 1

I get this error: line 2: syntax error near unexpected token '$#'' line 2: if [ ( '$#' -eq 1 ) && ( -f "$1" ) ]; then'

I have tried every combination of quotes (", ', no quotes) around the $# and I always get a variant of those error messages using the type of quotes I use. I can't figure out what it's looking for.

I'm using Debian 10 and getting an error in a simple bash script. All it does is check to see if 1 parameter is passed in, and if it's a file just echo out the file:

#!/usr/bin/bash
if [ ( '$#' -eq 1 ) && ( -f "$1" ) ]; then
        echo "$1"
fi

exit 1

I get this error:

line 2: syntax error near unexpected token `'$#''
line 2: `if [ ( '$#' -eq 1 ) && ( -f "$1" ) ]; then'

I have tried every combination of quotes (", ', no quotes) around the $# and I always get a variant of those error messages using the type of quotes I use. I can't figure out what it's looking for.

Source Link
raphael75
  • 753
  • 1
  • 11
  • 26
Loading