2

I'm trying to write a script that's part of a much bigger automation script that configures the GitHub ssh key on a local dev machine.

This is the line I'm trying to run but for some reason the 'eval $(ssh-agent -s)' fails as it errors and outputs this message.

'eval' is not recognized as an internal or external command,
operable program or batch file.
cmd.exe /c "ssh-keygen -t rsa -b 4096 -C "$githubEmailAddress" && eval $(ssh-agent -s) && ssh-add ~/.ssh/id_rsa && clip < ~/.ssh/id_rsa.pub"

I have looked around and I'm having no luck getting past this issue. I can't work out how to launch the Git Bash terminal where the command works from the ps1 script.

2 Answers 2

4

You should consider creating an alias for bash.exe that way it is only referenced one time in the script and will be easier to change if you need to in the future.

You can then create the SSH key as shown:

New-Alias -Name gitBash -Value "$Env:ProgramFiles\Git\bin\bash.exe

gitBash -c "ssh-keygen -t rsa -b 4096 -C "[email protected]" && eval $(ssh-agent -s) && ssh-add ~/.ssh/id_rsa && clip < ~/.ssh/id_rsa.pub && exit"

You also won't have to do as much encoding as the key gen parameters won't need to be wrapped in a string delimiter anymore.

You also avoid managing the current directory manually like you are with the cd command.

Also note the use of the $Env:ProgramFiles to get the base path of the program files directory, its not common for it to be configured differently than "C:\Program Files" but it can be and it avoids issues with spaces in the path name this way.

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

Comments

0

After a lot of trial and error the way you can open a git bash prompt from powershell is to find the bin\bash.exe file normally found in a folder called Git within the Program Files folder.

From the location of the ps1 file you might need to modify the ....\ part but the script below allow you to open a bash prompt execture a bunch of calls and then exit the prompt

cmd.exe /c 'cd "..\..\Program Files\Git\bin" && bash.exe -c "ssh-keygen -t rsa -b 4096 -C "[email protected]" && eval $(ssh-agent -s) && ssh-add ~/.ssh/id_rsa && clip < ~/.ssh/id_rsa.pub && exit"' 

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.