1
  animal_name=""

  if [[ -z "${!animal_name// }" ]]; then
   animal_name="doggo"
  fi
    
 echo sudo chmod ...... ${!animal_name}

Is there any way to reference a variable in bash?

3
  • Describe what you want to do and what the desired output is. Commented Aug 25, 2020 at 21:42
  • @Cyrus in the case the variable does not exist, I was the animal_name to be set to "doggo". However, it always ends up being blank when I try to echo Commented Aug 25, 2020 at 21:43
  • 4
    Set default content if variable is empty: echo "${animal_name:=doggo}" Commented Aug 25, 2020 at 21:49

4 Answers 4

3

Ditch the "{}"

animal_name=""
if [[ -z "$animal_name" ]]; then
    animal_name="doggo"
fi

sudo chmod {flag} $animal_name
Sign up to request clarification or add additional context in comments.

3 Comments

sorry made an edit. I have more thing happening in the echo. Can i still use this?
Sure thing. :) I'll edit the answer to show your use case
One question though - why use the echo? Are you trying to execute chmod or print out the whole "chmod {flag} $animal_name"?
0

I think your error is in line 3 (or 2, depending on how you look at it.)

animal_name=""

if [[ -z "${!animal_name// }" ]]; then # Error: event not found: animal_name//
animal_name="doggo"
fi

echo sudo chmod ...... $animal_name

The fix that I found was to remove the 2 slashes in "${!animal_name// }" and that worked for me.

2 Comments

Edited to include the chmod ....... Could you provide some more background? like do you have a file/executable named doggo? what's the purpose of sudo chmod ...... {!animal_name}?
oh i think im getting this mixed up with javascript... do you know what {!animal_name} means when I include {! }?
0

If you are just trying to make sure the variable has some value, you can use defaults.

echo sudo chmod ...... ${animal_name:-doggo}

This will use doggo is animal_name is empty, but will leave it empty.

$: animal_name=
$: echo sudo chmod ...... ${animal_name:-doggo}
sudo chmod ...... doggo
$: echo "[$animal_name]" # still empty
[]
$: animal_name=bob
$: echo sudo chmod ...... ${animal_name:-doggo} # doesn't change is already set
sudo chmod ...... bob

If you want animal_name to ALWAYS default to doggo thereafter, use = instead of - and it will assign it as well if it's empty.

echo sudo chmod ...... ${animal_name:=doggo}

so:

$: animal_name=
$: echo sudo chmod ...... ${animal_name:=doggo}
sudo chmod ...... doggo
$: echo "[$animal_name]" # this time it was set
[doggo]
$: animal_name=bob
$: echo sudo chmod ...... ${animal_name:=doggo} # doesn't change if already set
sudo chmod ...... bob

1 Comment

Heck, this is the answer Cyrus gave in the comments yesterday, lol...
0

Probably you mean indirection? This may help.

animal_name=""
doggo="I am doggo!"
if [[ -z "${animal_name}" ]]; then
  animal_name="doggo"
fi

echo sudo chmod ...... ${!animal_name}

The ${!animal_name} reference the value of doggo, so the output is:

sudo chmod ...... I am doggo!

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.