0

I recently started bash scripting and got stuck with a very basic usecase, searched stackoverflow/google but couldn't find a way to achieve what I am trying to do.

I have a script color.sh

#!/bin/bash
Apple="Red"
Orange="Orange"
Banana="Yello"
echo $$1

What I am trying to achieve is print the color of fruit and accept fruit from command line. The output I want is

./color.sh Apple -> Red, but what I get is some random number which I think is process Id.

leo017
  • 45
  • 9
  • What you want is called "indirect expansion" – Charles Duffy Oct 09 '20 at 18:13
  • In addition to the linked duplicate, see [BashFAQ #6](https://mywiki.wooledge.org/BashFAQ/006). And BTW, what you're getting is process id with a `1` appended to the end. (`echo $$1` => `$$` == process id, `1` == literal string). – Charles Duffy Oct 09 '20 at 18:14
  • ...and as another aside, `echo $foo` is inherently buggy for the reasons given in [BashPitfalls #14](http://mywiki.wooledge.org/BashPitfalls#echo_.24foo). Always quote your arguments: `echo "$foo"`, or in the present case, `echo "${!1}"` – Charles Duffy Oct 09 '20 at 18:15
  • Exactly what I was looking for, that a lot @CharlesDuffy – leo017 Oct 09 '20 at 18:19

0 Answers0