1

I am trying to use the sed command to edit a value in the .git/config file. When running the remote origin command, the config file creates the line of text like this":

url = https://github.com/abbatrombone/testprj

The name changes based on the username abbatrombone and project name testprj. I am trying to edit this so it contains the key value so I can commit via the terminal rather than have to go in and edit it. It would look something like this:

url = https://[email protected]/abbatrombone/testprj.git
    echo "paste you github key. Make sure it has the correct permissions"
    read -r gitkey

    echo "Username"
    read -r username

    echo "projectname"
    read -r projectname

    filepath="url = https://github.com/$username/$projectname"
    correctfilepath="url = https://[email protected]/$username/$projectname.git"
    
    git remote add origin https://github.com/"$username"/"$projectname"
    
    sed -i "s|${filepath}/${correctfilepath}|g" $Dir/.git/config

    git remote add origin main https://github.com/"$username"/"$projectname"; # first time updates git 2nd time runs it correctly
     
    git push -u origin main;

I have tried using double quotes over single quotes so variables expand properly, changing the delimiter to | from / and doing ${var} rather than $var.

How do I get the sed command to update this string correctly?

4
  • try sed -i "s|${filepath}|${correctfilepath}|g" $Dir/.git/config Commented Jul 5 at 2:32
  • that did not work its a file/path not file|path the reason i was using | is because from my understanding the character after s is the delimiter. Commented Jul 5 at 2:47
  • 1
    In your question, you have sed -i "s|${filepath}/${correctfilepath}|g" $Dir/.git/config. In this command, the delimiter | only appears twice. I believe the delimiter needs to occur three times. Commented Jul 5 at 4:36
  • What didn't work? Edit the question with proper syntax mentioned above and error/problem you're getting. "didn't work" doesn't help anyone including yourself.
    – Destroy666
    Commented Jul 5 at 5:59

1 Answer 1

2

In your question, you have the following.

sed -i "s|${filepath}/${correctfilepath}|g" $Dir/.git/config.

In this command, the delimiter | only appears twice. I believe the delimiter needs to occur three times. Try the following.

sed -i "s|${filepath}|${correctfilepath}|g" $Dir/.git/config

Actually, the {} are not needed. Below is a simplication.

sed -i "s|$filepath|$correctfilepath|g" $Dir/.git/config

Note that using the -i option without specifying an extension is not recommended (according to man sed) and is not always portable. For example, the above command would work under Ubuntu but not macOS. The equivalent to the above command under macOS would be as follows.

sed -i "" "s|$filepath|$correctfilepath|g" $Dir/.git/config

A more portable and perhaps better approach would be to allow for a backup, then remove the backup after the sed command completes. Below would work for both Ubuntu and macOS.

sed -i.bak "s|$filepath|$correctfilepath|g" $Dir/.git/config
rm -f $Dir/.git/config.bak

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .