1

How to defer the name variable in our Bash script so that it expands correctly within quotes in the loop, in such e.g:

name='*'
curate() {
  local e=
  for e in $(export HOME='/home/me'; echo /home/me/Downloads/\${name}.$1)
  do
    [ -e "$e" ] && echo "$e"
  done
}

curate() c

name=foo
curate() c

as it didn't work.
Please help in clear way

5
  • 2
    curate() c - Sidenote, functions are called simply by invoking their names, i.e. without (). Commented Dec 11, 2024 at 4:39
  • 1
    Please edit your question and add your desired output (no description) for that sample input. Commented Dec 11, 2024 at 6:11
  • What do you expect setting HOME in your command substitution to accomplsh for you? Commented Dec 11, 2024 at 17:59
  • What do you expect the echo command in that same process substitution to accomplish for you, especially given that you are escaping the first $? Commented Dec 11, 2024 at 18:01
  • The result of a command substitution is not subject to variable expansion (during the processing of the same command). If that's what you're trying to accomplish then you'll need to find a different way. Commented Dec 11, 2024 at 18:16

1 Answer 1

2

You can replace your curate function with:

curate() {
  local IFS= e=
  for e in /home/me/Downloads/$name.$1
  do
    [ -e "$e" ] && echo "$e"
  done
}

By localising IFS to empty string, glob expansion still happens but word-splitting of the result is disabled.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.