1

How do I use variables with dot in a Unix (Bash) shell script? I have the script below:

#!/bin/bash
set -x
"FILE=system.properties"
FILE=$1
echo $1
if [ -f "$FILE" ];
then
   echo "File $FILE exists"
else
   echo "File $FILE does not exist"
fi

This is basically what I need ⟶ x=propertyfile, and propertyfile=$1. Can someone please help me?

4
  • Is FILE=system.properties (without "") not working? Commented Aug 18, 2014 at 5:23
  • 1
    @Karthick - Posts need to be edited only to make them technically legible. You can not edit a grammatically correct post, and make it completely incorrect. Sorry but you have completely messed up my post. This just does not sound right grammatically. Commented Aug 18, 2014 at 5:23
  • if the words are not grammatically correct then how the members can understand or how you can expect the answer ??? Commented Aug 18, 2014 at 5:29
  • Exactly my point. Your edit was very messed up. I restored it back to what I originally had! Commented Aug 18, 2014 at 5:32

2 Answers 2

5

You can't declare variable names with dots but you can use associative arrays to map keys, which is the more appropriate solution. This requires Bash 4.0.

declare -A FILE                      ## Declare variable as an associative array.
FILE[system.properties]="somefile"   ## Assign a value.
echo "${FILE[system.properties]}"    ## Access the value.
Sign up to request clarification or add additional context in comments.

Comments

2

Note that the line:

"FILE=system.properties"

tries to execute a command FILE=system.properties which most likely doesn't exist. To assign to a variable, the quote must come after the equals:

FILE="system.properties"

It is a bit hard to tell from the question what you are after, but it sounds as if you might be after indirect variable names. Unfortunately, standard editions of bash don't allow dots in variable names.

However, if you used an underscore instead, then:

FILE="system_properties"
system_properties="$1"
echo "${FILE}"
echo "${!FILE}"

will echo:

system_properties
what-was-passed-as-the-first-argument

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.