Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gmmastros on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable declaration with "-" not working in KSH

Status
Not open for further replies.

tulsantide

Technical User
Jun 1, 2015
21
US
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
 
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
 
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..
 
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.

[URL unfurl="true" said:
https://www.tutorialspoint.com/unix/unix-using-variables.htm[/URL]]
The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _).
 
If you want to get pedantic, here's what the IEEE spec has to say...

[URL unfurl="true" said:
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html[/URL]]
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top