Score:1

"which" command output into a variable does not work

cg flag

I am just a beginner at bash scripting. I tried to save output from the command "which" into a variable and printing it but the variable shows nothing. This is the code which I tried:

#!/bin/bash
OUTPUT="$(which curl)"
echo "${OUTPUT}"

Output:

user@user:~$ bash new.sh

user@user:~$

Also this works when I run it in terminal.

user@user:~$ OUTPUT="$(which curl)"
echo "${OUTPUT}"

curl not found
user@user:~$

But the thing is it works with other commands.

#!/bin/bash
OUTPUT="$(date)"
echo "${OUTPUT}"

Output:

user@user:~$ bash new.sh
Sat 07 Aug 2021 01:41:37 PM +0545
user@user:~$
bac0n avatar
cn flag
Still a bit curious, the fact that the error message you entered can not come from `which`, it should not give any message at all actually if, e.g., curl does not exist?
Sammy1410 avatar
cg flag
Well, the output was "curl <and its version>" when it is found and it gave "curl not found" if not installed. But the latter output is seen in terminal only but not while using shell variables.
bac0n avatar
cn flag
if curl is not installed, e.i., if the binary does not exist, it should not print any message at all. You can open `which` in a text editor, it's a very simple shell script, the message "curl not found" has to come from somewhere else. (and if curl exists, it should return the path, not `curl` <version>)
Sammy1410 avatar
cg flag
Yes seems like it. This was just the small piece of code I was trying to put in if statement. Well, I found ```which``` command returns a boolean data that I could use. Thank you for helping. Finally, my code is running as expected.
Sammy1410 avatar
cg flag
And sorry my mistake ```which``` returned path not version. I mistook it with the other commands.
Score:4
cn flag

The variable only accepts the value sent through STDOUT, or the output stream. Since curl cannot be found, the output you are seeing is an error message sent through STDERR or the error stream. If you wish to store the error message in the variable in the case of an error., do the following:

OUTPUT="$(which curl 2>&1)"

This directs all data from STDERR to STDOUT. 2 is the file descriptor for STDERR and 1 is the file descriptor for STDOUT.

Sammy1410 avatar
cg flag
I tried but the result is still same.
Sammy1410 avatar
cg flag
As user bac0n suggested ```which``` did not give any output storable when the package was not found. I had to go the other way around for my main code to work.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.