18

I have a simple script (let's call it script.sh):

#!/bin/bash

source variables
echo $1

What I want to achieve is scripts output:

255.255.255.255

When executed like:

./script.sh VARIABLE

where VARIABLE is defined in variables file:

VARIABLE=255.255.255.255

Is it possible?

3 Answers 3

25

example:

#!/bin/bash

variable=255.255.255.255
param=$1
echo ${!param}

then execute:

bash script.sh variable
255.255.255.255

The exclamation mark makes param to get the value of the variable with that name. See about parameter indirection.

Sign up to request clarification or add additional context in comments.

2 Comments

could we use echo ${!$1}? nop
@jon_eldiablo: echo ${!1} shoud do fine.
4

Could you please try following it may help you in same.

./script.sh "$VARIABLE"

Comments

0

You have to add $ before variable name. For example: ./script.sh $VARIABLE

Is good to put argument in double quotes like: ./script.sh „$VARIABLE”

Writing from mobile phone, sorry for no using syntax.

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.