2

I know I can run two commands back-to-back in the command line:

git add -u && git commit --amend --no-edit

I also have aliases setup using doskey

doskey a=git add $*

But how can I combine multiple commands in a single doskey alias?

doskey amend=git add -u && git commit --amend --no-edit

This fails by setting the doskey to the first command doskey amend=git add -u and then proceeds to immediately execute git commit --amend --no-edit at runtime instead of saving the composite command to the doskey alias.

I also tried quoting the whole thing:

doskey amend="git add -u && git commit --amend --no-edit"

But this fails too because it interprets the quotes literally:

'"git add -u && git commit --amend --no-edit"' is not recognized as an internal or external command, operable program or batch file.

1

The problem is that && denotes the end of the doskey command and the second git command is executed in-line after it.

I have tried other syntax combinations, but none of them allows && to be parsed after the doskey expansion, apart from:

doskey amend=cmd /c "git add -u && git commit --amend --no-edit"

This launches a new shell, which is inefficient. A better solution might be to put both commands into a batch file (eg amend.cmd) and assign the macro to this:

doskey amend=amend.cmd
| improve this answer | |
9

This was the first result in the google but I found other similar question with nice answers.

1- escape the ampersand character doskey foo=bar ^& baz

2- use doskey's own command seperator doskey foo=bar $T baz

source

This is cleaner and does not open extra command windows.

| improve this answer | |
  • 2
    This is the correct answer, the one currently selected as correct has multiple issues. – ShaneB Nov 16 '19 at 11:03
0

You can chain commands within an individual DOSKEY, using as Ali mentions using the $T or ^& separators, however you cannot then chain the doskey command in your terminal. An example is in order

This works

doskey ls=dir $T echo YAY A FOLDER

See the output:

C:\tmp>ls
 Volume in drive C has no label.
 Volume Serial Number is ABCD-1234

 Directory of C:\tmp

23/07/2020  18:02    <DIR>          .
23/07/2020  18:02    <DIR>          ..
C:\tmp>YAY A FOLDER
C:\tmp>

This doesn't

C:\tmp>ls && echo YAY ANOTHER ECHO!!
 Volume in drive C has no label.
 Volume Serial Number is ABCD-1234

 Directory of C:\tmp

23/07/2020  18:02    <DIR>          .
23/07/2020  18:02    <DIR>          ..
C:\tmp>YAY A FOLDER
C:\tmp>

BOO there's no YAY ANOTHER ECHO!!

A better solution

Use batch files in a folder and set your PATH

  1. Put dir && echo YAY A FOLDER in a file C:\batch\ls.bat
  2. Set path=%PATH%;C:\batch
  3. Run ls && echo YAY ANOTHER ECHO!!
  4. Admire the lovely output

_

C:\tmp>ls && echo YAY ANOTHER ECHO!!
 Volume in drive C has no label.
 Volume Serial Number is ABCD-1234

 Directory of C:\tmp

23/07/2020  18:02    <DIR>          .
23/07/2020  18:02    <DIR>          ..
YAY A FOLDER
YAY ANOTHER ECHO!!

C:\tmp>

PS - for step 2 above better to permanently set your PATH environment variable.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.