Search code examples
fish

How to run multiple commands at once asynchronously in fish shell?


In bash, I would run

(npm install -g npm@latest && npm install -g neovim bash-language-server vscode-langservers-extracted graphql -language-service-cli solidity-language-server typescript-language-server)& to install some packages I need.

I have been trying to do the same command in fish and am having trouble. I am thinking of creating a separate file and run it with bash, but would be nice how to do this in fish. I am getting a fish: Command substitutions not allowed error. I can run this

npm install -g npm@latest && npm install -g neovim bash-language-server vscode-langservers-extracted graphql -language-service-cli solidity-language-server typescript-language-server, but it is not async. I have a bunch of other commands like this, hence async would be faster

Thanks


Solution

  • When you place commands in parenthesis in Bash (or any POSIX shell), you are running them in a subshell, with the & of course placing the subshell in the background.

    Fish doesn't have the exact concept of a subshell, but for your particular example, you can accomplish the same goal by running in a sub-process rather than a subshell.

    fish -c "npm install -g npm@latest && npm install -g neovim bash-language-server vscode-langservers-extracted graphql -language-service-cli solidity-language-server typescript-language-server" &
    

    There are several differences (and maybe others) between a subshell and a sub-process like this, but they don't matter for this particular scenario.