Getting a tool version from asdf
Context
The following command prints a tool version managed by asdf:
Unfortunately there’s no way to get only the version. However that could be achieved with additional output manipulation.
Using sed and cut commands a version of given tool could be extracted using the following pipeline:
Explanation:
- Print current tool version with
asdf current golang
. - In the output from asdf replace with one space character any sub-strings consisting of multiple space characters with
sed -e "s/ \{2,\}/ /g"
. - Split resulted output on columns separated by one space character and return the second column with
cut -d" " -f2
.
In this form the one-liner doesn’t work well when given tool isn’t installed yet. In that case asdf doesn’t print anything to the output stream, but instead sends an error message to the error stream. Like in the following example with Node.js:
To fix the issue error stream needs to be redirected to the output stream. The final version of the one-liner looks like this:
UPDATE: There’s a way to replace sed and cut with awk to simplify the solution.
- TIL rating:
- good to know