Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash - variable variables [duplicate]

Tags:

variables

bash

I have the variable $foo="something" and would like to use:

bar="foo"; echo $($bar) 

to get "something" echoed.

like image 521
user1165454 Avatar asked May 25 '12 15:05

user1165454


People also ask

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

Is bash 1 True or false?

There are no Booleans in Bash. However, we can define the shell variable having value as 0 (“ False “) or 1 (“ True “) as per our needs.

What is $1 and $2 in bash?

$0 is the name of the script itself (script.sh) $1 is the first argument (filename1) $2 is the second argument (dir1)


1 Answers

In bash, you can use ${!variable} to use variable variables.

foo="something" bar="foo" echo "${!bar}"  # something 
like image 140
dAm2K Avatar answered Sep 24 '22 14:09

dAm2K