×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Variable declaration with "-" not working in KSH

Variable declaration with "-" not working in KSH

Variable declaration with "-" not working in KSH

(OP)
Hi,

The following is not working in KSH. Is there a restriction in KSH to not to use "-" in the variable declaration? The same script is working with "under score" though.

#!/bin/ksh
######################################################################
x-y_HOSTLIST=server1,server2
echo "List is :${x-y_HOSTLIST}"
============================

I'm getting the following error
=========================
x-y_HOSTLIST=server1,server2: not found [No such file or directory]
List is :y_HOSTLIST

Please let me know on how to fix the issue.

Thanks

RE: Variable declaration with "-" not working in KSH

Hi

Are you sure this is a Ksh issue ? That does not work in other Bourne compatible shells either, like Bash, Dash, Fish, Yash and Zsh. Only works in Elvish, but based on the rest of your code, that is clearly not the one you had in mind. So better use underscore.

Feherke.
feherke.github.io

RE: Variable declaration with "-" not working in KSH

(OP)
Thanks Feherke for the response.
We have a requirement to use "-" during the declaration of a variable. Please let me know if you know any other ways to do it.

Thanks..

RE: Variable declaration with "-" not working in KSH

You will need to change your requirements then. A dash character is not a valid character in a variable name. It has special meaning to pretty much all shells.

There is no other way to do it. Even if you do find some wonky way to finagle a dash into your variable names, you will probably end up with a script that is difficult, if not impossible to maintain.

Quote (https://www.tutorialspoint.com/unix/unix-using-var...)


The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _).

RE: Variable declaration with "-" not working in KSH

If you want to get pedantic, here's what the IEEE spec has to say...

Quote (https://pubs.opengroup.org/onlinepubs/9699919799/b...)


Environment variable names used by the utilities in the Shell and Utilities volume of POSIX.1-2017 consist solely of uppercase letters, digits, and the <underscore> ( '_' ) from the characters defined in Portable Character Set and do not begin with a digit.


RE: Variable declaration with "-" not working in KSH

(OP)
I got it Sam. Thanks for the response.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close