0

I work with CentOS and the only shell I work with is Bash.

I am trying to develop a Bash script which configures a content management system and I seek a way to make this script web application directory agnostic so that script users would be prompted about their web application directory path instead of me presenting them an hardcoded directory path which might be different than the one in their environment.

The only way I know to prompt data from a user and store it in a variable is read but I have a problem using it because the data might be variables and the current release of read can't naturally produce variables.


  • read web_application_root

$HOME/www

Brings '$HOME/www' (string) instead HOME_DIRECTORY_NAME/www (expanded variable).

This situation is a problem for me by itself but it creates a bigger problem for me in case the user input the prompt with even more complicated variable structures such as:

  • read domain
  • read domain_dir

$web_application_root/$domain/public_html


How to prompt a user for variables without read?

  • 3
    How is that different from your other question: How to expand variables inside read? – Stéphane Chazelas Feb 22 '21 at 12:13
  • @StéphaneChazelas after you told me there that I might have an XY problem, cluing it is because I focus on read, ilkkachu came and only strengthened this understanding of mine; to prevent an XY problem I present what I try to achieve and my problem without focusing on keep using read as a solution and just ask what will be a solution. – variableexpander Feb 22 '21 at 12:20
  • It's is not clear from the question whether you program is an interactive script that will always be run by a human, or whether it may be conceivable that the script could be executed non-interactively too, for example when it's part of a larger system. Depending on the complexity of the data that needs to be inputted into the script, command line arguments or a separate configuration file (in JSON, for example) may be better suited than reading data from the keyboard. – Kusalananda Feb 22 '21 at 12:26
  • @Kusalananda it is indeed an interactive script that should always be executed by a human on a direct/dedicated SSH session aimed to backup and upgrade a content management system. I prefer not to store the data on JSON but rather to take an all Bash approach. – variableexpander Feb 22 '21 at 12:33
  • Unless I must take a serializing approach as with JSON and alike, that is. – variableexpander Feb 22 '21 at 12:41
  • As far as I see, Stéphane showed you in the answer to the other question how to use envsubst to expand variables in the string you got with read. Even accepting that you want to prompt the user for the value, is there some reason that doesn't work for you either? – ilkkachu Feb 22 '21 at 12:46
  • @ilkkachu I am having hard time to find my way in Stéphane's answer because it has lots of code I am not familiar with; I don't even know if he meant that I should execute one of the functions and then use read regularly as I originally intended... – variableexpander Feb 22 '21 at 13:24
  • I tried to ask a better question, or is it now worse?... – variableexpander Feb 22 '21 at 13:27
  • @variableexpander, I don't think you're going to really find a way to prompt the user for something, and then have any variable references in the input expanded, in one step. (Possibly partly because it's not often anyone wants to do that.) That is, the user doesn't input variables, they input strings of characters. What you do with those strings is an orthogonal matter (take them as-is, interpret them as numbers, modify them by some rule, e.g. by expanding stuff that looks like $var). All that means you may need to look deeper into the subject since you need to do some of it by hand. – ilkkachu Feb 22 '21 at 13:32
  • as for Stéphane's answer under the other question, the first code block looks like it should work as-is (if you copy it to a script and run it), there's the read shown, and you can input something like $HOME/somedir and see what the ls shows (or, what ends up in the variable). Now, granted, he's also showing some good practices there, e.g. handling the possible error from read (if there is no input), and setting IFS and using read -r to prevent it from mangling the input. There's also a link as to why you should want to do the latter two. – ilkkachu Feb 22 '21 at 13:36
  • @ilkkachu thanks; I should re-read the answer carefully; about All that means you may need to look deeper into the subject since you need to do some of it by hand I didn't understand what you meant by that. – variableexpander Feb 22 '21 at 14:02
  • @StéphaneChazelas also, in this question I am not interested only in expanding environment variables but variables in general (indeed, my last question gave only an environment variable example but it wasn't really my intention). – variableexpander Feb 22 '21 at 14:27
  • @variableexpander, I did show a variant that performs all shell words expansions as opposed to just env var expansions in that other Q&A. I suggest you close/delete this Q&A and we focus in addressing your questions in that other Q&A to avoid duplication here. – Stéphane Chazelas Feb 22 '21 at 15:27
  • @StéphaneChazelas I am glad to know you are still eager to help me. I feel lost with the other answer, please refer me to that particular variant, I will do my best to re read about it and if any question comes up I will comment there. – variableexpander Feb 22 '21 at 15:28
  • See the read_one_line_and_perform_shell_word_expansions function there. – Stéphane Chazelas Feb 22 '21 at 15:32
  • 1
  • 1
    Sorry, yes, I support a merge. – variableexpander Feb 23 '21 at 16:39

0 Answers0