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
?
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 usingread
as a solution and just ask what will be a solution. – variableexpander Feb 22 '21 at 12:20envsubst
to expand variables in the string you got withread
. 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:46read
regularly as I originally intended... – variableexpander Feb 22 '21 at 13:24$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:32read
shown, and you can input something like$HOME/somedir
and see what thels
shows (or, what ends up in the variable). Now, granted, he's also showing some good practices there, e.g. handling the possible error fromread
(if there is no input), and settingIFS
and usingread -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:36All 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:02read_one_line_and_perform_shell_word_expansions
function there. – Stéphane Chazelas Feb 22 '21 at 15:32