Perhaps I have misunderstood Stéphane Chazelas's answer here so I ask another question.
In Bash desire to expand variables inside read
without envsubst
, arguments or heredocuments (and without any non shell-builtins) because I work in a shared hosting environment in which I don't have many common non shell-builtin utilities.
The variable I want to expand is the environment variable HOME
but I make this question variable-agnostic, i.e. I seek a way in which the user can expand inside read:
- An environment variable
- A non environment variable
- Both types of variables
I have tried this which failed:
IFS= read -r domain_dir
${HOME}/example
echo ${domain_dir}
Current output:
${HOME}/example
Desired output:
MY_HOME_DIRECTORY/example
How to expand variables inside read without envsubst
, arguments or heredocuments (and without any non shell-builtins)?
Edit to explain why I find marking this a duplicate is wrong
According to the user terdon which marked this question duplicate, in this question I ask for something which is impossible.
If terdon is correct in that regard than this question is not a duplicate and can be answered separately with an answer that explains if and why it is indeed impossible.
read
and I can't imagine any reason to do so. You should always avoid prompting the user to enter text since that means the script cannot be automated and it is also very fragile since it is easy to make a typo. I am closing this as a duplicate since the answer is already given in your other question. You can't do what you are attempting. – terdon Mar 19 '21 at 09:18read
so those workarounds will be your best bet. Even better, don't make interactive scripts if you can possibly avoid them since they are so hard to use and easy to get wrong. Additionally, you never need to read$HOME
it is already available in the script. I edited Stéphane's answer to make it clear thatread
alone cannot do this. – terdon Mar 19 '21 at 10:18you never need to read $HOME it is already available in the script
; in this case I don't create a script file --- I just copy-paste some 7-8 commands from GitHub directly to the terminal and some command parts are data that I don't want to expose in public in GitHub (directory names which also include a website domain) hence the need for querying the user (myself) about these. Well, seems I do need to look for another way then rather thanread
. Thanks. – variable_expander Mar 19 '21 at 10:29$HOME
? Just use it directly in your script. I mean, you canecho "$HOME"
in any script and it will work. That's how environment variables work. But yes, I suggest you ask a question with a concrete example. I really can't imagine any reason you would have to use read here. – terdon Mar 19 '21 at 10:31$HOME
because in my shared hosting environment the user names are very long and non intuitive, such askjdfkjtyyyckhtttt
; I host there 10 years already and up until about 4 months it wasn't like this; after they upgraded their systems and servers some things have changed. – variable_expander Mar 19 '21 at 10:37$HOME
to your script, your script already knows$HOME
. Just tryecho $HOME
in any script and you will see. But if this isn't clear, please post a new question, where you explain what you actually need to do instead of focusing on a specific approach for it (likeread
). – terdon Mar 19 '21 at 10:39read
expand the variables by itself, you would have gotten an answer to that effect in the first question. Given that there was no such an answer, and you must have read Bash's manual forread
and seen it doesn't describe such a feature, it's hard to tell what more you're looking for. Surely you're not asking someone to go chasing the source code to prove that no such feature exists? – ilkkachu Mar 19 '21 at 10:47echo $HOME
:) I just want to get its value into a variable without memorizing or copy-pasting it and then stored into a variable. Okay, I will try a new question... I hope it goes well... – variable_expander Mar 19 '21 at 10:50