Skip to main content
added 6 characters in body
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441
find . ... -execdir zsh -c "git remote get-url origin $(git ...)" \;
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Here, the command substitution is inside a double-quoted string, and so is expanded in the main shell, before find runs. Then it runs in the wrong directory, leading to the error. If you enable set -x and run the command, you should see a constant string there in the zsh -c command line.

Like so:

$ set -x
$ find . -type d -execdir bash -c "echo $RANDOM" \;
+ find . -type d -execdir bash -c 'echo 22924' ';'
22924
22924
...

You'll need to use single quotes to protect the expansions so the inner shell process sees them and gets to process them.

$ find . -type d -execdir bash -c 'echo $RANDOM' \;
+ find . -type d -execdir bash -c 'echo $RANDOM' ';'
4397
21235
...

I think you can mostly just flip the single and double quotes around here since the sed command doesn't contain anything that necessarily needs to be within single quotes:

find . -type d -name ".git" -execdir \
   zsh -c 'git remote set-url origin "$(
               git remote get-url origin | 
               sed "s|https://github|https://my_work_username@github|g"
           )"' \;

(For completeness, also put double quotes around the command substitution to prevent word splitting in the inner shell command. Not that the URLs here are likely to contain whitespace or glob characters, but it's good practice.)

If the sed command had something like $ in it, and you needed to put single quotes within a single-quoted string, you'd have do uglier escaping like 'it isn'\''t so', or just split the command in multiple files which often works as an easy workaround for quoting issues.

find . ... -execdir zsh -c "git remote get-url origin $(git ...)" \;
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Here, the command substitution is inside a double-quoted string, and so is expanded in the main shell, before find runs. Then it runs in the wrong directory, leading to the error. If you enable set -x and run the command, you should see a constant string there in zsh -c command line.

Like so:

$ find . -type d -execdir bash -c "echo $RANDOM" \;
+ find . -type d -execdir bash -c 'echo 22924' ';'
22924
22924
...

You'll need to use single quotes to protect the expansions so the inner shell process sees them and gets to process them.

$ find . -type d -execdir bash -c 'echo $RANDOM' \;
+ find . -type d -execdir bash -c 'echo $RANDOM' ';'
4397
21235
...

I think you can mostly just flip the single and double quotes around here since the sed command doesn't contain anything that necessarily needs to be within single quotes:

find . -type d -name ".git" -execdir \
   zsh -c 'git remote set-url origin "$(
               git remote get-url origin | 
               sed "s|https://github|https://my_work_username@github|g"
           )"' \;

(For completeness, also put double quotes around the command substitution to prevent word splitting in the inner shell command. Not that the URLs here are likely to contain whitespace or glob characters, but it's good practice.)

If the sed command had something like $ in it, and you needed to put single quotes within a single-quoted string, you'd have do uglier escaping like 'it isn'\''t so', or just split the command in multiple files which often works as an easy workaround for quoting issues.

find . ... -execdir zsh -c "git remote get-url origin $(git ...)" \;
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Here, the command substitution is inside a double-quoted string, and so is expanded in the main shell, before find runs. Then it runs in the wrong directory, leading to the error. If you enable set -x and run the command, you should see a constant string there in the zsh -c command line.

Like so:

$ set -x
$ find . -type d -execdir bash -c "echo $RANDOM" \;
+ find . -type d -execdir bash -c 'echo 22924' ';'
22924
22924
...

You'll need to use single quotes to protect the expansions so the inner shell sees them and gets to process them.

$ find . -type d -execdir bash -c 'echo $RANDOM' \;
+ find . -type d -execdir bash -c 'echo $RANDOM' ';'
4397
21235
...

I think you can mostly just flip the single and double quotes around here since the sed command doesn't contain anything that necessarily needs to be within single quotes:

find . -type d -name ".git" -execdir \
   zsh -c 'git remote set-url origin "$(
               git remote get-url origin | 
               sed "s|https://github|https://my_work_username@github|g"
           )"' \;

(For completeness, also put double quotes around the command substitution to prevent word splitting in the inner shell command. Not that the URLs here are likely to contain whitespace or glob characters, but it's good practice.)

If the sed command had something like $ in it, and you needed to put single quotes within a single-quoted string, you'd have do uglier escaping like 'it isn'\''t so', or just split the command in multiple files which often works as an easy workaround for quoting issues.

Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

find . ... -execdir zsh -c "git remote get-url origin $(git ...)" \;
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Here, the command substitution is inside a double-quoted string, and so is expanded in the main shell, before find runs. Then it runs in the wrong directory, leading to the error. If you enable set -x and run the command, you should see a constant string there in zsh -c command line.

Like so:

$ find . -type d -execdir bash -c "echo $RANDOM" \;
+ find . -type d -execdir bash -c 'echo 22924' ';'
22924
22924
...

You'll need to use single quotes to protect the expansions so the inner shell process sees them and gets to process them.

$ find . -type d -execdir bash -c 'echo $RANDOM' \;
+ find . -type d -execdir bash -c 'echo $RANDOM' ';'
4397
21235
...

I think you can mostly just flip the single and double quotes around here since the sed command doesn't contain anything that necessarily needs to be within single quotes:

find . -type d -name ".git" -execdir \
   zsh -c 'git remote set-url origin "$(
               git remote get-url origin | 
               sed "s|https://github|https://my_work_username@github|g"
           )"' \;

(For completeness, also put double quotes around the command substitution to prevent word splitting in the inner shell command. Not that the URLs here are likely to contain whitespace or glob characters, but it's good practice.)

If the sed command had something like $ in it, and you needed to put single quotes within a single-quoted string, you'd have do uglier escaping like 'it isn'\''t so', or just split the command in multiple files which often works as an easy workaround for quoting issues.