Skip to main content
deleted 5 characters in body
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k

The first command line argument is available as $1. A valid shell variable name starts with a alphabetic character (or underscore) and continues with alphanumerics (or underscores).

Two shell patterns that matches an invalid shell variable name is

[!a-zA-Z_]*

and

[a-zA-Z_]*[!a-zA-Z_0-9]*

You may use case ... esac to do pattern matching against a variable's value.

Spoiler warning:

#!/bin/sh
LC_ALL=C
case "$1" in
    [!a-zA-Z_]*|[a-zA-Z_]*[!a-zA-Z_0-9]*|_|""9]*|"")
        echo 'NO'
        ;;
    *)
        echo 'YES'
esac
This also answers "NO" for a variable whose name is just an underscore, or whose name is empty. Note that this is using shell globbing patterns, not regular expressions, and that it runs in any POSIX shell, not just bash.

Testing:

$ ./script.sh _ae
YES
$ ./script.sh 0a9oe
NO
$ ./script.sh aoeat
YES
$ ./script.sh aoeat-aoe
NO

The first command line argument is available as $1. A valid shell variable name starts with a alphabetic character (or underscore) and continues with alphanumerics (or underscores).

Two shell patterns that matches an invalid shell variable name is

[!a-zA-Z_]*

and

[a-zA-Z_]*[!a-zA-Z_0-9]*

You may use case ... esac to do pattern matching against a variable's value.

Spoiler warning:

#!/bin/sh
case "$1" in
    [!a-zA-Z_]*|[a-zA-Z_]*[!a-zA-Z_0-9]*|_|"")
        echo 'NO'
        ;;
    *)
        echo 'YES'
esac
This also answers "NO" for a variable whose name is just an underscore, or whose name is empty. Note that this is using shell globbing patterns, not regular expressions, and that it runs in any POSIX shell, not just bash.

Testing:

$ ./script.sh _ae
YES
$ ./script.sh 0a9oe
NO
$ ./script.sh aoeat
YES
$ ./script.sh aoeat-aoe
NO

The first command line argument is available as $1. A valid shell variable name starts with a alphabetic character (or underscore) and continues with alphanumerics (or underscores).

Two shell patterns that matches an invalid shell variable name is

[!a-zA-Z_]*

and

[a-zA-Z_]*[!a-zA-Z_0-9]*

You may use case ... esac to do pattern matching against a variable's value.

Spoiler warning:

#!/bin/sh
LC_ALL=C
case "$1" in
    [!a-zA-Z_]*|[a-zA-Z_]*[!a-zA-Z_0-9]*|"")
        echo 'NO'
        ;;
    *)
        echo 'YES'
esac
This also answers "NO" for a variable whose name is empty. Note that this is using shell globbing patterns, not regular expressions, and that it runs in any POSIX shell, not just bash.

Testing:

$ ./script.sh _ae
YES
$ ./script.sh 0a9oe
NO
$ ./script.sh aoeat
YES
$ ./script.sh aoeat-aoe
NO
added 6 characters in body
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k

The first command line argument is available as $1. A valid shell variable name starts with a alphabetic character (or underscore) and continues with alphanumerics (or underscores).

Two shell patterns that matches an invalid shell variable name is

[!a-zA-Z_]*

and

[a-zA-Z_]*[!a-zA-Z_0-9]*

You may use case ... esac to do pattern matching against a variable's value.

Spoiler warning:

#!/bin/sh
case "$1" in
    [!a-zA-Z_]*|[a-zA-Z_]*[!a-zA-Z_0-9]*|_|"")
        echo 'NO'
        ;;
    *)
        echo 'YES'
esac
This also answers "NO" for a variable whose name is just an underscore, or whose name is empty. Note that this is using shell globbing patterns, not regular expressions, and that it runs in any POSIX shell, not just bash.

Testing:

$ ./script.sh _ae
YES
$ ./script.sh 0a9oe
NO
$ ./script.sh aoeat
YES
$ ./script.sh aoeat-aoe
NO

The first command line argument is available as $1. A valid shell variable name starts with a alphabetic character (or underscore) and continues with alphanumerics (or underscores).

Two patterns that matches an invalid shell variable name is

[!a-zA-Z_]*

and

[a-zA-Z_]*[!a-zA-Z_0-9]*

You may use case ... esac to do pattern matching against a variable's value.

Spoiler warning:

#!/bin/sh
case "$1" in
    [!a-zA-Z_]*|[a-zA-Z_]*[!a-zA-Z_0-9]*|_|"")
        echo 'NO'
        ;;
    *)
        echo 'YES'
esac
This also answers "NO" for a variable whose name is just an underscore, or whose name is empty. Note that this is using shell globbing patterns, not regular expressions, and that it runs in any POSIX shell, not just bash.

Testing:

$ ./script.sh _ae
YES
$ ./script.sh 0a9oe
NO
$ ./script.sh aoeat
YES
$ ./script.sh aoeat-aoe
NO

The first command line argument is available as $1. A valid shell variable name starts with a alphabetic character (or underscore) and continues with alphanumerics (or underscores).

Two shell patterns that matches an invalid shell variable name is

[!a-zA-Z_]*

and

[a-zA-Z_]*[!a-zA-Z_0-9]*

You may use case ... esac to do pattern matching against a variable's value.

Spoiler warning:

#!/bin/sh
case "$1" in
    [!a-zA-Z_]*|[a-zA-Z_]*[!a-zA-Z_0-9]*|_|"")
        echo 'NO'
        ;;
    *)
        echo 'YES'
esac
This also answers "NO" for a variable whose name is just an underscore, or whose name is empty. Note that this is using shell globbing patterns, not regular expressions, and that it runs in any POSIX shell, not just bash.

Testing:

$ ./script.sh _ae
YES
$ ./script.sh 0a9oe
NO
$ ./script.sh aoeat
YES
$ ./script.sh aoeat-aoe
NO
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k

The first command line argument is available as $1. A valid shell variable name starts with a alphabetic character (or underscore) and continues with alphanumerics (or underscores).

Two patterns that matches an invalid shell variable name is

[!a-zA-Z_]*

and

[a-zA-Z_]*[!a-zA-Z_0-9]*

You may use case ... esac to do pattern matching against a variable's value.

Spoiler warning:

#!/bin/sh
case "$1" in
    [!a-zA-Z_]*|[a-zA-Z_]*[!a-zA-Z_0-9]*|_|"")
        echo 'NO'
        ;;
    *)
        echo 'YES'
esac
This also answers "NO" for a variable whose name is just an underscore, or whose name is empty. Note that this is using shell globbing patterns, not regular expressions, and that it runs in any POSIX shell, not just bash.

Testing:

$ ./script.sh _ae
YES
$ ./script.sh 0a9oe
NO
$ ./script.sh aoeat
YES
$ ./script.sh aoeat-aoe
NO