Geuis
Geuis

Reputation: 42267

How to use strings in subsequent bash commands?

I'm pretty unfamiliar with bash syntax and I'm running into a problem of how to use strings from a previous operation as inputs into subsequent ones. Here's what I have now:

#!/bin/bash

searchTerm=$1

results=$(npm search $searchTerm --json | jq '.[].name')

for term in $results
do
  info=$(npm info $term)

  echo "####" $info
done

The command that executes looks like the following and fails:

npm info "\"exampleTerm\""

How do I use $term in the for loop in combination with npm info?

Upvotes: 0

Views: 46

Answers (2)

Shammel Lee
Shammel Lee

Reputation: 4475

A (long) one-liner will do it…

npm search react-redux --json | jq '.[].name' | xargs -I pkg sh -c 'echo "#### $(npm info $1)"' - pkg

Upvotes: 1

Geuis
Geuis

Reputation: 42267

Answer is to use eval like the following:

#!/bin/bash

searchTerm=$1

results=$(npm search $searchTerm --json | jq '.[].name')

for term in $results
do
  info=$(eval npm info "$term")

  echo "####" $info
done

Upvotes: 0

Related Questions