0

I host a .md file with a few Bash code lines in my GitHub account and would like to copy-paste these lines and execute them, one by one, to upgrade software (a website module) non automatically on purpose;
These Bash code lines work on a website directory which in my case is under:

$HOME/MEDIUM_DIRECTORY_1/DOMAIN_DIRECTORY/MEDIUM_DIRECTORY_2.

I don't want to publish the actual path in GitHub so I seek how to interactively query the user (currently only myself but in the future not necessarily so) directly in the terminal, for such a path.

In my shared hosting environment, user names for SSH are very long and random such as jktantttopppazqqqddd so it would be inefficient for me to memorize them or to copy-paste them from the terminal each time anew, but the rest of the path is easy for me to remember; this is why I wanted to prompt myself with "${HOME}" as a variable expansion and then store it along with the rest of the path in a variable --- just before copy-pasting and executing the Bash code lines (with which I would utilize such a variable).

I tried to use something like read website_dir and give the path above where ${HOME} is a variable expansion as with "${HOME}"/MEDIUM_DIRECTORY_1/DOMAIN_DIRECTORY/MEDIUM_DIRECTORY_2, but it wasn't fruitful because one cannot expand variables inside read.

I would define what I am trying to do as efficiently creating a path to work with before copy-pasting and executing commands from a file, in the terminal, one by one and I seek the most efficient way to achieve that which principally must include some automation in printing the long and randomly created username (without copy pasting it from terminal or from anywhere else, such as echo $HOME).

How would you solve the problem of creating a path with the username automatically?

Update

This worked for me:

home_dir=$(echo $HOME) &&
read website_dir_relative_path_after_home_dir && 
website_dir="${home_dir}${website_dir_relative_path_after_home_dir}" # Note the lack of spacing between the two ${}${};

Please share how you would do it.

4
  • Please register your account so you can actually delete your posts instead of having to ask us to do it for you.
    – terdon
    Mar 19, 2021 at 12:50
  • I prefer not to but please don't worry about this - it shouldn't happen again. BTW, I think a delete button should appear. Mar 19, 2021 at 13:01
  • It does appear if you register. If you refuse to register, you will not be able to manage your posts.
    – terdon
    Mar 19, 2021 at 13:08
  • Not in the deletion context, which I personally find odd (I asked myself why prevent from people such as myself who avoid registering into websites in general to do so, but of course, that's for another discussion). Mar 19, 2021 at 13:09

1 Answer 1

0

$HOME is a variable. You don't need a new one. So if you know that the target directory is $HOME/MEDIUM_DIRECTORY_1/DOMAIN_DIRECTORY/MEDIUM_DIRECTORY_2, then that's all you need in your script:

full_path="$HOME/MEDIUM_DIRECTORY_1/DOMAIN_DIRECTORY/MEDIUM_DIRECTORY_2"

For example, consider this script:

#!/bin/bash
full_path="$HOME/MEDIUM_DIRECTORY_1/DOMAIN_DIRECTORY/MEDIUM_DIRECTORY_2"
echo "Path is: $full_path"

Running that gives:

$ foo.sh
Path is: /home/terdon/MEDIUM_DIRECTORY_1/DOMAIN_DIRECTORY/MEDIUM_DIRECTORY_2

As you can see, there is no need to ask for $HOME, it is already available inside your script by default.

If you do need input from the user, don't prompt for it! That is error prone and annoying to the user. Just take it from the command line:


full_path="$1"
## rest of the script

You would then call the script with scriptName.sh $HOME/MEDIUM_DIRECTORY_1/DOMAIN_DIRECTORY/MEDIUM_DIRECTORY_2 and then the variable $full_path in the script will be set to $HOME/MEDIUM_DIRECTORY_1/DOMAIN_DIRECTORY/MEDIUM_DIRECTORY_2.

Finally, if you must prompt the user for some obscure reason I cannot imagine, you can prompt for a relative path. For example:

echo 'Please enter the relative path to the target directory.'
read -p "the path you enter will be assumed to be relative to $HOME: " full_path
echo "You entered: $full_path, moving there now"
cd "$full_path"
echo "We are currently in $PWD"

Running that would give:

$ foo.sh
Please enter the relative path to the target directory.
the path you enter will be assumed to be relative to /home/terdon: foo
You entered: foo, moving there now
We are currently in /home/terdon/foo
2
  • Thanks ! So my entire saga was about ~ instead ${HOME} wasn't it? I should learn more about the difference between the two... Mar 19, 2021 at 13:10
  • @variable_expander no, sorry, I shouldn't have mentioned the ~, that has another set of problems. My point was that $HOME is always defined you don't need to ask for it, you don't need to put it into another variable, you can simply use it. newVar=$(echo $HOME); cd "$HOME" is exactly the same as simply doing cd "$HOME" directly.
    – terdon
    Mar 19, 2021 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.