suresh thota
suresh thota

Reputation: 21

How to create a variable with (-) hyphen as one of the character in it in shell scripting

I have a requirement of using a variable as abc-def which i am passing as a parameter and want to use in the shell script.

ex:

#!/bin/bash
abc-def="xyz"
echo "$abc-def"

there is an hyphen in the variable, i will have to use abc-def as a parameter and script needs to understand it wherever i will use.

Upvotes: 2

Views: 2562

Answers (2)

glenn jackman
glenn jackman

Reputation: 247022

With a suitable recent bash, you can create an associative array, and use the "variable" name as an array key:

#!/bin/bash

declare -A vars
name="abc-def"
value=xyz
vars["$name"]=$value
echo "${vars["$name"]}"

Upvotes: 1

PaulProgrammer
PaulProgrammer

Reputation: 17670

You don't.

Variable names used by the utilities in the Shell and Utilities volume of IEEE Std 1003.1-2001 consist solely of upper and lowercase letters, digits, and the '_' (underscore) from the characters defined in Portable Character Set and do not begin with a digit. Other characters may be permitted by an implementation; applications shall tolerate the presence of such names.

Upvotes: 4

Related Questions