Use bash $ HOME in a shell script
How do I make bash execute a variable value. For example, we have this code, where the value of the variable is specified in single quotes (!).
#!/bin/bash
V_MY_PATH='$HOME'
echo "$V_MY_PATH"
ls $V_MY_PATH
Output signal
$HOME
ls: $HOME: No such file or directory
How do I make bash to translate the insto shell variable to its value, if any.
I want to add code after V_MY_PATH = '$ HOME' to make an output like echo $ HOME.
This is something simple, but I'm stuck. (NB: I know that with V_MY_PATH = "$ HOME" it works great.)
EDIT PART: I just wanted to keep it simple, but I feel like some details are needed.
I am getting a parameter from a file. This part works well. I don't want to rewrite it. The problem is that when my V_MY_PATH contains a predefined variable (like $ home) it is not treated as its value.
source to share
use an indirect reference variable like so:
pete.mccabe@jackfrog$ p='HOME'
pete.mccabe@jackfrog$ echo $p
HOME
pete.mccabe@jackfrog$ ls ${p}
ls: cannot access HOME: No such file or directory
pete.mccabe@jackfrog$ ls ${!p}
bash libpng-1.2.44-1.el6 python-hwdata squid
...
pete.mccabe@jackfrog$
The value of $ {! p} means $ p value and this value is the name of the variable whose contents I want to refer to
source to share
Remove single quotes
V_MY_PATH='$HOME'
it should be
V_MY_PATH=$HOME
you want to use $HOME
as a variable
you cannot have variables in single quotes.
End script:
#!/bin/bash
V_MY_PATH=$HOME
echo "$V_MY_PATH"
ls "$V_MY_PATH" #; Add double quotes here in case you get weird filenames
Output:
/home/myuser 0 05430142.pdf 4 aiSearchFramework-7_10_2007-v0.1.rar
and etc.
source to share
-
You can use single or double quotes, and in your case, none if you like.
-
You can't tell bash anything what a variable is equal to. Maybe something like this? (if I don't understand what you are trying to do)
================================================== ====================
#!/bin/bash
#########################
# VARIABLES #
#########################
V_MY_PATH=home
#########################
# SCRIPT #
#########################
echo "What is your home path?"
read $home
echo "Your home path is $V_MY_PATH"
ls $V_MY_PATH
Of course, you can also just delete the variable at the top and use: echo "Your home path is $ home"
source to share