0

I am trying to get a folder name from a stored variable string. When I ran the following

path="Folder%20Name/Dir/File"
read -d "/" folder < <(echo ${path/\%20/ })
echo "$folder"

I am getting a blank echo $folder. Where am I going wrong, I have tried read -d "/" folder <<< $"(${path/\%20/ })" with no sucess

closed as off-topic by ilkkachu, Stephen Kitt, Isaac, Jeff Schaller, dhag Dec 12 '18 at 14:34

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – ilkkachu, Stephen Kitt, Isaac, Jeff Schaller, dhag
If this question can be reworded to fit the rules in the help center, please edit the question.

  • I get Folder Name. – pLumo Dec 12 '18 at 9:08
  • I just realized I was running it in a script and then doing echo outside the script. That was what was causing the blanks – Bret Joseph Dec 12 '18 at 9:17
0

Your first command works just fine on Bash 4.4:

$ path="Folder%20Name/Dir/File"
$ read -d "/" folder < <(echo ${path/\%20/ })
$ echo "$folder"
Folder Name

Though using process substitution here is unnecessary, you could just use a here-string instead:

$ read -d "/" folder <<< "${path/\%20/ }"

As for your second command, you're using the localization quoting $"...", which I don't think you need here, and the parenthesis also get added to the string, so you'd get (Folder Name if you did that with the path variable.

(as an aside, if you ever start using Zsh, don't use path as the name of a variable.)

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