0

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.

  • I don't understand what the difference is between this and your previous question. You cannot expand variables in 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:18
  • Hello @terdon are you sure it's impossible with the common versions of Bash? (version 5.x to latest)? The reason I want to prompt for data which includes expanded variables is to make interactivity for myself. This is a case in which I don't want full automation. – variable_expander Mar 19 '21 at 09:40
  • I think it's incorrect to mark this a duplicate if Stéphane's answers doesn't include a way to do what I seek --- rather, what you wrote above should be the answer to the current question. I don't think Stéphane wrote or even clued that it's impossible (rather, possible and problematic). – variable_expander Mar 19 '21 at 09:41
  • I have tried to edit the question to explain why I think marking this a duplicate is wrong. – variable_expander Mar 19 '21 at 10:02
  • Stéphane has given you various workarounds but variables aren't expanded in read 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 that read alone cannot do this. – terdon Mar 19 '21 at 10:18
  • @terdon thank you for the edit and the clarifications. About one thing you said: you 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 than read. Thanks. – variable_expander Mar 19 '21 at 10:29
  • Why would you query $HOME? Just use it directly in your script. I mean, you can echo "$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
  • @terdon I wanted to use $HOME because in my shared hosting environment the user names are very long and non intuitive, such as kjdfkjtyyyckhtttt ; 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
  • OK. So use it. I am trying to explain that you don't need to pass $HOME to your script, your script already knows $HOME. Just try echo $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 (like read). – terdon Mar 19 '21 at 10:39
  • @variable_expander, if there was a way to get read 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 for read 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:47
  • I know that about echo $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

0 Answers0