Skip to main content
added 109 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Quoting.

In the script, use "$1" rather than just $1.

On the command line, use

./script '*(ontehu'

instead of

./script *(ontehu

  • ./script.sh (abc This is a syntax error in the shell grammar.

  • ./script.sh && This makes the shell think there is a conditional AND on the command line and it expects something on the right hand side of the && operator.

  • ./script.sh &abc This is two commands: ./script started as a background process (with &), and the command abc.

In all these cases, the argument should be quoted (single quoted, ideally, unless you need the shell to insert the value of a shell variable, in which case it should be double quoted).


Your script:

if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
  echo it matches
else
  echo does_not match
fi

Quote $1:

if [[ "$1" =~ ^[A-Za-z_]+$ ]]; then
  echo it matches
else
  echo does_not match
fi

Allow digits in the tail end of the value:

if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
  echo it matches
else
  echo does_not match
fi

Do proper reporting of errors (this is extra):

if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
  printf '"%s" is a valid variable name\n' "$1"
else
  printf '"%s" is not a proper variable name\n' "$1" >&2
  exit 1
fi

Quoting.

In the script, use "$1" rather than just $1.

On the command line, use

./script '*(ontehu'

instead of

./script *(ontehu

  • ./script.sh (abc This is a syntax error in the shell grammar.

  • ./script.sh && This makes the shell think there is a conditional AND on the command line and it expects something on the right hand side of the && operator.

  • ./script.sh &abc This is two commands: ./script started as a background process (with &), and the command abc.

In all these cases, the argument should be quoted (single quoted, ideally).


Your script:

if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
  echo it matches
else
  echo does_not match
fi

Quote $1:

if [[ "$1" =~ ^[A-Za-z_]+$ ]]; then
  echo it matches
else
  echo does_not match
fi

Allow digits in the tail end of the value:

if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
  echo it matches
else
  echo does_not match
fi

Do proper reporting of errors (this is extra):

if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
  printf '"%s" is a valid variable name\n' "$1"
else
  printf '"%s" is not a proper variable name\n' "$1" >&2
  exit 1
fi

Quoting.

In the script, use "$1" rather than just $1.

On the command line, use

./script '*(ontehu'

instead of

./script *(ontehu

  • ./script.sh (abc This is a syntax error in the shell grammar.

  • ./script.sh && This makes the shell think there is a conditional AND on the command line and it expects something on the right hand side of the && operator.

  • ./script.sh &abc This is two commands: ./script started as a background process (with &), and the command abc.

In all these cases, the argument should be quoted (single quoted, ideally, unless you need the shell to insert the value of a shell variable, in which case it should be double quoted).


Your script:

if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
  echo it matches
else
  echo does_not match
fi

Quote $1:

if [[ "$1" =~ ^[A-Za-z_]+$ ]]; then
  echo it matches
else
  echo does_not match
fi

Allow digits in the tail end of the value:

if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
  echo it matches
else
  echo does_not match
fi

Do proper reporting of errors (this is extra):

if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
  printf '"%s" is a valid variable name\n' "$1"
else
  printf '"%s" is not a proper variable name\n' "$1" >&2
  exit 1
fi
added 400 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Quoting.

In the script, use "$1" rather than just $1.

On the command line, use

./script '*(ontehu'

instead of

./script *(ontehu

  • ./script.sh (abc This is a syntax error in the shell grammar.

  • ./script.sh && This makes the shell think there is a conditional AND on the command line and it expects something on the right hand side of the && operator.

  • ./script.sh &abc This is two commands: ./script started as a background process (with &), and the command abc.

In all these cases, the argument should be quoted (single quoted, ideally).


Your script:

if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
  echo it matches
else
  echo does_not match
fi

Quote $1:

if [[ "$1" =~ ^[A-Za-z_]+$ ]]; then
  echo it matches
else
  echo does_not match
fi

Allow digits in the tail end of the value:

if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
  echo it matches
else
  echo does_not match
fi

Do proper reporting of errors (this is extra):

if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
  printf '"%s" is a valid variable name\n' "$1"
else
  printf '"%s" is not a proper variable name\n' "$1" >&2
  exit 1
fi

Quoting.

In the script, use "$1" rather than just $1.

On the command line, use

./script '*(ontehu'

instead of

./script *(ontehu

  • ./script.sh (abc This is a syntax error in the shell grammar.

  • ./script.sh && This makes the shell think there is a conditional AND on the command line and it expects something on the right hand side of the && operator.

  • ./script.sh &abc This is two commands: ./script started as a background process (with &), and the command abc.

In all these cases, the argument should be quoted (single quoted, ideally).

Quoting.

In the script, use "$1" rather than just $1.

On the command line, use

./script '*(ontehu'

instead of

./script *(ontehu

  • ./script.sh (abc This is a syntax error in the shell grammar.

  • ./script.sh && This makes the shell think there is a conditional AND on the command line and it expects something on the right hand side of the && operator.

  • ./script.sh &abc This is two commands: ./script started as a background process (with &), and the command abc.

In all these cases, the argument should be quoted (single quoted, ideally).


Your script:

if [[ $1 =~ ^[A-Za-z_]+$ ]]; then
  echo it matches
else
  echo does_not match
fi

Quote $1:

if [[ "$1" =~ ^[A-Za-z_]+$ ]]; then
  echo it matches
else
  echo does_not match
fi

Allow digits in the tail end of the value:

if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
  echo it matches
else
  echo does_not match
fi

Do proper reporting of errors (this is extra):

if [[ "$1" =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]; then
  printf '"%s" is a valid variable name\n' "$1"
else
  printf '"%s" is not a proper variable name\n' "$1" >&2
  exit 1
fi
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Quoting.

In the script, use "$1" rather than just $1.

On the command line, use

./script '*(ontehu'

instead of

./script *(ontehu

  • ./script.sh (abc This is a syntax error in the shell grammar.

  • ./script.sh && This makes the shell think there is a conditional AND on the command line and it expects something on the right hand side of the && operator.

  • ./script.sh &abc This is two commands: ./script started as a background process (with &), and the command abc.

In all these cases, the argument should be quoted (single quoted, ideally).