user2727195 user2727195 - 4 months ago 10
Linux Question

command line pipe syntax on terminal

I'm executing following commands in a nodejs application using child processes.


  1. which zip
    to get path of the zip executable

  2. path-to-zip + " -r abc.zip " + path + " && du -hs abc.zip"
    using ampersand
    &&



how can we chain these commands together to form one single command so that
path-to-zip
gets substituted from 1)
which zip
.

I'm using
which zip
to ensure path is correct, tell me if it's useless if the path
/usr/bin/zip
has been the same and going to be the same across linux and mac os, if that's the case then I'm better off with hardcoding the path in my application, please advise.

Answer

It depends on your shell.

Bash is the the most common shell in Linux distribution.

For Bash, there are two common ways which are called Command Substitution to achieve this.

  1. `which zip` + whatever you want (`means`, I type `due to the syntax policy of StackOverflow.)

  2. $(which zip) + whatever you want

Manual of Command Substitution :

https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html

Comments