I've looked at Quoting within $(command substitution) in Bash and still don't get what I do incorrectly (my code looks to me like "is the recommended way" from accepted answer) and how to fix it:
print_and_run(){
echo next line: "$1"
echo "$($1)"
}
print_and_run 'sed --in-place -- "s|fetch = +refs/*:refs/*|fetch = +refs/heads/:refs/remotes/origin/|" .git/config'
next line: sed --in-place -- "s|fetch = +refs/*:refs/*|fetch = +refs/heads/:refs/remotes/origin/|" .git/config
sed: -e expression #1, char 1: unknown command: `"'
sed
line works on its own, function print_and_run
works for commands w/out quotes, e.g print_and_run 'cat ./.git/config'
. TIA
P.S. btw not sure it matters: I wrote echo "$($1)"
not echo $($1)
to print with newlines: https://stackoverflow.com/questions/15184358/how-to-avoid-bash-command-substitution-to-remove-the-newline-character, and now I see the former looks like "is the recommended way").
echo "$(foo)"
, it's almost the same as justfoo
, except that it removes any trailing newlines from the output and then adds exactly one – ilkkachu Dec 13 '22 at 15:38