0

I have a variable:

my_var="$('/some/path/to/username/more/path' 'and/something/else')"

and I want to change it to be more generic like:

my_var="$('/some/path/to/${USER}/more/path' 'and/something/else')" 

but that doesn't work. How can I get the user inside of there?

SumNeuron
  • 4,850
  • 5
  • 39
  • 107
  • `my_var=$("/some/path/to/${USER}/more/path" "and/something/else")` The outer quotes aren't necessary but the inner ones are if the args contain whitespace that should be preserved. – Jeff Holt Feb 17 '22 at 13:52

1 Answers1

0

The problem is that inside a single quoted string nothing is treated with a special meaning. This makes them good for scripting, eg. grep '^pattern$' ... and sed 's/abc$/def/'.

You can use double quotes instead.

$(...) is a command substitution. Is that correct? If so, then you can nest double qoutes:

my_var="$("/some/path/to/${USER}/more/path" 'and/something/else')" 

This should be interpreted as two set of quotes, so the outer double quotes, isn't stopped at the first inner quote:

my_var="$(                                                      )" # First set of quotes
          "/some/path/to/${USER}/more/path"                        # Second set of quotes

When assigning a variable, you don't need to wrap the expression in double quotes, so:

my_var=$("/some/path/to/${USER}/more/path" 'and/something/else')

would also be fine in this case. Most other cases you should always wrap parameter expansions ($abc), command substitutions ($(...)) etc. in double quotes as to avoid word splitting and pathname expansions.

However to me it seems like you are trying to create an array instead?

$ my_var=("/some/path/to/${USER}/more/path" 'and/something/else')
$ echo "${my_var[0]}"
/some/path/to/and/more/path
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • Thank you for the answer, I am not sure if it will work though? Basically I want to change what Conda injects: `__conda_setup="$('/Users/username/miniforge3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"` and I am not sure removing the outer quotes or using double quotes twice will work – SumNeuron Feb 17 '22 at 14:43