Sample Header Ad - 728x90

Cannot source file using find, fzf, and xargs

3 votes
1 answer
958 views
I am trying to use fzf to select and activate different virtual environments. However, I am receiving the follow error:
xargs: source: No such file or directory

My command comprises three parts: find | fzf | xargs
(1) find locates my virtual environments,
(2) a virtual environment is selected using fzf, and
(3) xargs is used to source the selected file.

Below is the full command:
find ~/.virtualenvs/ -maxdepth 1 -type d | fzf | xargs -I {} source {}/bin/activate
xargs: source: No such file or directory

I can directly source the file (i.e., command prompt shows entry into fzf_delete virtual environment):
brian:~$ source /home/brian/.virtualenvs/fzf_delete/bin/activate
(fzf_delete) brian:~$

I can select the virtual environment using fzf:
find ~/.virtualenvs/ -maxdepth 1 -type d | fzf
/home/brian/.virtualenvs/fzf_delete

I have also isolated the issue to xargs by removing the dependency on find and fzf:
echo "/home/brian/.virtualenvs/fzf_delete" | xargs -I {} source {}/bin/activate
xargs: source: No such file or directory

I have tried every variation of quoting, use of variables, string concatenation, command substitution, using bash -c, etc. that I can think of to no avail.

**How can I get xargs to source the selected file?**
PS - I am aware that null termination should be used with find, fzf, and xargs in case file names contain spaces, but that does not appear to be causing the immediate issue.
Asked by user2514157 (225 rep)
Jun 15, 2022, 11:12 PM
Last activity: Jun 16, 2022, 05:40 AM